最初のAPIリクエスト https://docs.hazbase.com/start/quickstart/ GET STARTED 最初のAPIリクエスト 公開APIから登録済みトークンの一覧を取得します。SDKのインストールからAPIへの接続、レスポンスの読み方、エラーへの対応までを紹介します。 @hazbase/kit 0.9.0 Node.js 22 / JavaScript (ESM) 読み取りのみ・秘密鍵不要 0 1 環境を準備 0 2 SDKを入れる 0 3 APIを呼ぶ 0 4 結果を確かめる 1. 実行環境を準備する この手順はNode.js 22を使います。ターミナルでバージョンを確認し、新しいディレクトリを作成してください。APIの利用先は既定の https://api.hazbase.com です。 Terminal 1 2 3 4 5 6 7 node --version npm --version mkdir hazbase-day1 cd hazbase-day1 npm init -y npm pkg set type = module npm install --save-exact @hazbase/kit@0.9.0 TypeScriptの設定やtsxの追加は不要です。まずJavaScriptで接続を確認し、その後で必要に応じてTypeScriptやReactへ展開できます。 2. 最初のリクエストを実行する 次のコードを index.mjs として保存し、実行します。chainId=11155111はSepoliaを指定します。取得する一覧は、その環境に登録されたトークンです。チェーン全体のトークン検索ではありません。 index.mjs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import { createHazbaseWalletClient , HazbaseWalletApiError } from "@hazbase/kit/wallet" ; // Read-only: no private key, signature, transfer, or transaction is used. const apiEndpoint = process . env . HAZBASE_API_ENDPOINT || "https://api.hazbase.com" ; const chainId = Number ( process . env . CHAIN_ID || "11155111" ) ; if ( ! Number . isSafeInteger ( chainId ) || chainId <= 0 ) throw new Error ( "CHAIN_ID must be a positive integer." ) ; const wallet = createHazbaseWalletClient ( { apiEndpoint , fetcher : ( url , options ) => { const headers = new Headers ( options ?. headers ) ; if ( process . env . HAZBASE_API_KEY ) headers . set ( "X-Hazbase-Api-Key" , process . env . HAZBASE_API_KEY ) ; return fetch ( url , { ... options , headers , signal : AbortSignal . timeout ( 15000 ) } ) ; } , } ) ; try { const result = await wallet . listTokens ( { chainId } ) ; console . log ( "Connected to:" , apiEndpoint ) ; console . log ( "Chain ID:" , chainId ) ; console . log ( "Tokens returned:" , result . tokens . length ) ; console . table ( result . tokens . map ( ( { name , symbol , standard , decimals , transferable } ) => ( { name , symbol , standard , decimals , transferable , } ) ) , ) ; if ( result . tokens . length === 0 ) console . log ( "Connection succeeded. No tokens are configured for this chain." ) ; } catch ( error ) { if ( error instanceof HazbaseWalletApiError ) { console . error ( "HTTP status:" , error . status , "Code:" , error . code ?? "not provided" ) ; } console . error ( error instanceof Error ? error . message : String ( error ) ) ; process . exitCode = 1 ; } コードをダウンロード Terminal node index.mjs 3. 成功したことを確認する 「Connected to」「Chain ID」「Tokens returned」が表示され、取得したトークンが表で出力されれば成功です。件数と名称は環境の登録内容によって変わります。0件でもAPIへの接続は成功しています。 応答構造の例(0件の場合) 1 2 3 { "tokens" : [ ] } tokensにはname、symbol、address、standard、decimals、transferableなどが含まれます。返却項目と型はHTTP APIリファレンスで確認できます。 GET /api/wallet/tokens SDKを使わずHTTPで確認する 同じ読み取りはcurlでも実行できます。このリクエストにはAuthorizationヘッダーや秘密鍵を付けません。 bash 1 2 3 curl --fail-with-body --max-time 15 \ "https://api.hazbase.com/api/wallet/tokens?chainId=11155111" \ -H "Accept: application/json" 次に進む ここまででSDKから実APIへの接続を確認できました。発行・移転を行う場合には、利用する環境、権限、コントラクトやウォレットの準備を次のガイドで確認してください。 実装方法を選ぶ うまく動かない場合