LoginSignup
1
1

More than 1 year has passed since last update.

kabuステーションAPIをTypescriptから呼び出す環境設定

Last updated at Posted at 2021-09-30

概要

kabuステーションAPIの呼び出しはpythonで作成したほうが何かと便利だと思いますが
あえてTypeScript+axios+aspidaで呼び出す環境を作成します

必要な環境・ソフトウェア

  • linux等のコンソール(WSLも可、ただしアクセス方法がやや難しい)
  • VisualStudioCode(あれば楽)
  • node(ver12以降)
  • npm or yarn

構築手順

1.ディレクトリ・ファイル作成

mkdir kabucomapi
mkdir kabucomapi/src
mkdir kabucomapi/src/api
touch kabucomapi/src/getToken.ts

2.必要なパッケージのインストール

cd kabucomapi
yarn init -y
yarn add -D @types/node @types/aspida typescript ts-node
yarn add @aspida/axios axios
yarn tsc --init --rootDir src --outDir dist

3.型定義ファイル作成

kabuステーションAPIのopenAPI定義をaspida定義ファイルに変換します

cd ./src
npx openapi2aspida -i https://raw.githubusercontent.com/kabucom/kabusapi/master/reference/kabu_STATION_API.yaml

src/api以下にkabucomapi用のaspida定義ファイルが作成されます

4.テスト用ソース作成

※サンプルです。APIのパスワードはソースに含めないでください

src/getToken.ts
import aspida from "@aspida/axios"
import api from "./api/$api"

const data = { 'APIPassword': 'xxx' };
const url = 'http://localhost:18080/kabusapi/token';

;(async () => {
  try {
    const client = api(aspida())
    const res = await client.token.post({ body: data })
    console.log(res.body.ResultCode)
    console.log(res.body.Token)
  } catch(err) {
    console.log(err)
  }
})()

5.tsコンパイル・実行

npx tsc
node dist/getToken

WSLから呼び出したい場合

  • windows側にnginxをインストールしてリバプロさせる(リクエストヘッダのホスト名を書き換える)
  • src/api/$api.tsのアドレスをwindowsのipに書き換える

まとめ

aspidaすごい

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1