
# TypeScript {/* #typescript */}

[REST ガイド](/docs/ja/api/rest)に従って Platform API トークンを作成します。この例は Node.js 18 以降など、`fetch` を利用できるサーバー上で実行します。トークンはブラウザーのコードではなくサーバーに保存してください。

```typescript
type Catalog = {catalogId: string; name: string; kind: string};
type CatalogDetail = Catalog & {description: string};
type CatalogPage = {catalogs?: Catalog[]; nextPageToken?: string};

const baseUrl = process.env.CR_RECONSTRUCT_REST_URL ?? 'https://reconstruct.dev/api/v1/platform';
const token = process.env.CR_RECONSTRUCT_API_TOKEN;
if (!token) throw new Error('Set CR_RECONSTRUCT_API_TOKEN');

async function getJson<T>(path: string): Promise<T> {
  const response = await fetch(new URL(path, `${baseUrl.replace(/\/$/, '')}/`), {
    headers: {Authorization: `Bearer ${token}`},
  });
  if (!response.ok) throw new Error(`API returned HTTP ${response.status}`);
  return response.json() as Promise<T>;
}

async function main() {
  const page = await getJson<CatalogPage>('catalogs?pageSize=10');
  for (const catalog of page.catalogs ?? []) {
    const detail = await getJson<CatalogDetail>(
      `catalogs/${encodeURIComponent(catalog.catalogId)}`,
    );
    console.log(detail.catalogId, detail.description);
  }
  if (page.nextPageToken) console.log('Next page token:', page.nextPageToken);
}

main().catch(console.error);
```

次のページを取得するには、`nextPageToken` を URL エンコードしたクエリパラメーター `pageToken` として渡します。
