# SDKs

[Create an API key in TraceFast →](https://tracefast.xyz/settings/api/keys)

The official TraceFast SDKs call the [REST API](/api) from TypeScript, Python
and Rust. They are generated from the OpenAPI catalogue, so every method of the
catalogue a release was built from is there, typed, under the name and in the
group it has in these docs. The [case methods](/api/cases) came after 0.1.0
and are not in it yet. They are
open source under the MIT licence:
[github.com/datanestllc/tracefast-sdk](https://github.com/datanestllc/tracefast-sdk).

| Language | Install | Runs on |
|---|---|---|
| TypeScript / JavaScript | `npm install @tracefast_xyz/sdk` | Node.js 20.19+ or 22.12+, Bun, Deno, edge runtimes |
| Python | `pip install tracefast` | Python 3.10+, with a sync and an `asyncio` client |
| Rust | `cargo add tracefast` | Rust 1.88+, on Tokio |

Each package's README is its full reference:
[TypeScript](https://github.com/datanestllc/tracefast-sdk/tree/main/typescript#readme),
[Python](https://github.com/datanestllc/tracefast-sdk/tree/main/python#readme),
[Rust](https://github.com/datanestllc/tracefast-sdk/tree/main/rust#readme).

## Before you start

* **A key.** The SDKs call the API with an [API key](/api/authentication),
  sent in the `x-api-key` header and nowhere else — never in a URL, a log or an
  error.
* **Your server, not a browser.** The key is a secret: anyone who opens a page
  that holds it can read it.
* **A plan per method.** Each method needs **Enriched Data**, **Intelligence**
  or **Investigation**, and every higher plan includes it — see
  [Plans](/api/authentication#plans). Each method's doc comment names its plan
  and links to its page here.

## Quick start

The newest block and one transaction, with the key in `TRACEFAST_API_KEY`:

```ts
import { TraceFast } from '@tracefast_xyz/sdk'

const tf = new TraceFast({ apiKey: process.env.TRACEFAST_API_KEY! })

const { data: block } = await tf.blocks.latest()
console.log(block.number, block.hash)

const { data: tx } = await tf.transactions.get('0x6bb3103eec4ca8f4a324bbab71dc84f533af7e6be11ab8b13d0d10333bb9abc5')
console.log(tx.from, '→', tx.to, tx.valueWei, 'wei')
```

```python
import os

from tracefast import TraceFast

with TraceFast(api_key=os.environ["TRACEFAST_API_KEY"]) as tf:
    block = tf.blocks.latest()["data"]
    print(block["number"], block["hash"])

    tx = tf.transactions.get("0x6bb3103eec4ca8f4a324bbab71dc84f533af7e6be11ab8b13d0d10333bb9abc5")["data"]
    print(tx["from"], tx.get("to"), tx["valueWei"])
```

```rust
use tracefast::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(std::env::var("TRACEFAST_API_KEY")?)?;

    let latest = client.blocks().latest().await?;
    println!("block {} {}", latest.data.number, latest.data.hash);

    let tx = client
        .transactions()
        .get("0x6bb3103eec4ca8f4a324bbab71dc84f533af7e6be11ab8b13d0d10333bb9abc5", &tracefast::TransactionsGetParams::new())
        .await?;
    println!("{} sent {} wei", tx.data.from, tx.data.value_wei);
    Ok(())
}
```

## What every SDK does

The three SDKs follow one behaviour contract:

* **The whole answer.** A method returns the `{ data, meta }` envelope of
  [Conventions](/api/conventions); `meta.asOfBlock` says which block the
  answer is true at.
* **Absent is not `null`.** The types keep a missing field, `null` and a value
  apart, as the API does. A fact that is absent is not proven — it is not a
  "no".
* **Errors to branch on.** Every failure is one error type whose kind follows
  the status — authentication, plan, not found, the data cannot answer, rate
  limit and so on — and carries the API's `code`, `reason`, `details`,
  `retryAfter` and the request id to quote to support. Branch on `code` and
  `reason`, never on the message — see [errors](/api/conventions#errors).
* **Careful retries.** Only answers that say "try again" are retried: a `429`
  with a short `Retry-After`, a `503`, and a connection that broke. A `429`
  that waits longer is returned at once, with the real wait. A timeout is not
  retried.
* **Pagination.** Each paged method has two walkers: one gives whole pages,
  one gives rows, each with a limit. A walker follows `nextCursor` until it is
  absent — see [pagination](/api/conventions#pagination).
* **Nothing extra.** No telemetry and no caching; a redirect is never
  followed.

For example, a plan refusal and a walk over an address's transactions in
TypeScript:

```ts
import { PermissionDeniedError } from '@tracefast_xyz/sdk'

try {
  for await (const tx of tf.addresses.transactionsItems(
    '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
    { limit: 100 },
    { maxItems: 500 },
  )) {
    console.log(tx.hash)
  }
} catch (e) {
  if (e instanceof PermissionDeniedError && e.reason === 'plan_scope_missing') {
    console.log('needs', e.details?.minimumPlan, '— this key has', e.details?.currentPlan)
  } else throw e
}
```

In Python the walkers are `<method>_pages` and `<method>_items`; in Rust,
`<method>_pages` and `<method>_items` on the method's group.

## Versions

The three packages share one version and one
[changelog](https://github.com/datanestllc/tracefast-sdk/blob/main/CHANGELOG.md).
Before 1.0, a minor version may change a package's API; from 1.0 they follow
semantic versioning. Each version is generated from one version of the API
catalogue; methods marked **Beta** may still change on the API side — see
[stability](/api/conventions#stability).

Questions and bugs go to the
[repository's issues](https://github.com/datanestllc/tracefast-sdk/issues);
for a failed call, include the error's request id.

## Related pages

* [API keys](/api/authentication) — the key, plans and refusals
* [Conventions](/api/conventions) — the envelope, errors and pagination
* [All methods](/api/methods) — every method on one page
* [Using AI](/api/using-ai) — what to give an AI assistant or agent
