
# TypeScript

Start with the [REST guide](../rest.md) to create a Platform API token. This example runs on a server with `fetch` available, such as Node.js 18 or later. Keep the token on the server rather than in browser code.

```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);
```

Pass `nextPageToken` as the URL-encoded `pageToken` query parameter to fetch the next page.
