# TypeScript SDK

The Arche TypeScript SDK is a thin, typed client for the same public routes
documented in the [API reference](https://docs.arche.fi/reference). It runs on Node.js 18 or later
and any runtime with a standard `fetch`, and it is licensed under Apache 2.0.

## Install

```bash
npm install arche-sdk
```

## Create a client

```ts
import { Arche } from 'arche-sdk'

const arche = new Arche({ apiKey: process.env.ARCHE_API_KEY })
```

- `apiKey` (string | undefined): Sent as the `X-Api-Key` header. When omitted, the client reads the
  `ARCHE_API_KEY` environment variable. Create a key in the
  [developer portal](https://app.arche.fi/keys).
- `baseUrl` (string): Defaults to `https://api.arche.fi`.
- `fetch` (typeof fetch): Supply your own `fetch` when you need custom retries, proxies, or timeouts.
  Defaults to the global `fetch`.

## Methods and types

Every public route is a method named for its OpenAPI `operationId`, with typed
path, query and body arguments and a typed response envelope. Every schema is
exported as a type under `Schemas`. The methods and types are generated from the
published OpenAPI document, and a new version is released whenever that document
changes.

## The quickstart flow in TypeScript

This mirrors the [Quickstart](https://docs.arche.fi/quickstart) step for step.

```ts
import { Arche, snapshotHashOf } from 'arche-sdk'

const arche = new Arche()

// Step 2 - resolve a ticker to a canonical CIK
const resolved = await arche.resolveCompany({ ticker: 'MSFT' })

// Step 3 - list statements for that issuer
const statements = await arche.listStatements(resolved.data.cik, {
  statement_type: 'INCOME_STATEMENT',
  page: 1,
  page_size: 10,
})
console.log(statements.total, 'statements')

// Step 4 - ask what was knowable on a historical date
const pointInTime = await arche.getAsOfFinancials('0000320193', '2024-03-31')
console.log(snapshotHashOf(pointInTime))
```

Derived metrics come from the same client:

```ts
const series = await arche.getDerivedMetricsTimeSeries({
  ciks: ['0000320193'],
  statement_type: 'INCOME_STATEMENT',
  metrics: ['GROSS_MARGIN'],
  frequency: 'annual',
})
console.log(series.data.points.length)
```

## Replay tokens and snapshot hashes

A point-in-time query returns a replay token that anyone can re-check against
the same data. `replayTokenOf` and `snapshotHashOf` find them in any response,
`verifyReplayToken` re-runs a token, and `verifyUrl` gives a public page that
checks it without a key.

```ts
import { Arche, replayTokenOf, verifyUrl } from 'arche-sdk'

const arche = new Arche()

const { data } = await arche.resolveCompanyAsOf({ ticker: 'AAPL' })
const executed = await arche.executeQuery({
  query_type: 'financials_as_of_page',
  parameters: { cik: data.identity.cik, limit: 1, latest_only: true },
  as_of_date: '2024-11-02',
})
const token = replayTokenOf(executed)!
const verification = await arche.verifyReplayToken(token)
console.log(verifyUrl(token))
```

See [Verifiable agent answers](https://docs.arche.fi/guides/verifiable-agent-answers) for what a verification checks.

## Error handling

A failed request throws `ArcheApiError`, carrying the
[error envelope](https://docs.arche.fi/errors)'s `status`, `code`, `traceId` and `details`.

```ts
import { Arche, ArcheApiError } from 'arche-sdk'

const arche = new Arche()

try {
  await arche.resolveCompany({ ticker: 'MSFT' })
} catch (error) {
  if (error instanceof ArcheApiError) {
    console.log(error.status, error.code, error.traceId)
  }
  throw error
}
```

Log the `traceId` with any failure you report; it identifies the request on
Arche's side.

## Next steps

- [Python SDK](https://docs.arche.fi/sdks/python): Install the typed Python client and run the quickstart flow in code.
- [LangChain](https://docs.arche.fi/sdks/langchain): Give a LangChain agent the Arche tools, built on the Python SDK.
- [Overview](https://docs.arche.fi/): Start here for the product model, first request path, and key entry points.
