
# SDK quickstart

Install the SDK, set one key and get your first reply from a model, in about five minutes.

## Install the SDK

**TypeScript**

Use a supported Node.js LTS release (see `engines` in [`package.json`](https://github.com/ArtificialAnalysis/Tokedex/blob/main/typescript/package.json)) and an ESM project, with `"type": "module"` in `package.json`.

In your project directory, add the SDK:

```ts
npm install @tokedex/sdk
npm install --save-dev tsx
```

```python
uv add tokedex
```

```rust
cargo add tokedex
cargo add tokio --features macros,rt-multi-thread
```

**Python**

It needs Python 3.11 or later and [uv](https://docs.astral.sh/uv/).

**Rust**

It needs Rust 1.88 or later.

## Set a provider key

**OpenAI Responses, OpenAI Chat Completions, Anthropic Messages**

```bash
export OPENAI_API_KEY='<your-openai-api-key>'
```

**Gemini GenerateContent**

```bash
export GEMINI_API_KEY='<your-gemini-api-key>'
```

The Router reads each provider's key from the environment when a call needs it. [Provider keys](/sdk/provider-keys) lists every provider's variable.

> **Tip:** Keep provider keys in the environment, never in your source code. Your program then holds no secret, and a key cannot reach version control with it.

## Write a first program

**OpenAI Responses**

```ts
import { Router } from '@tokedex/sdk';

const router = new Router();

const response = await router.responses.create({
  model: 'openai/gpt-6-luna',
  input: 'Reply with exactly the word "hello".',
});
for (const item of response.output) {
  if (item.type !== 'message') continue;
  for (const part of item.content) if (part.type === 'output_text') console.log(part.text);
}
```

```python
from tokedex import Router

with Router() as router:
    response = router.responses.create(
        model="openai/gpt-6-luna", input='Reply with exactly the word "hello".'
    )
    print(response.output_text)
```

```rust
use tokedex::{Router, wire};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let router = Router::builder().build()?;
    let request = wire::ResponsesRequest::new("openai/gpt-6-luna", r#"Reply with exactly the word "hello"."#);
    let response = router.responses().create(request).await?;
    println!("{}", response.output_text());
    Ok(())
}
```

**OpenAI Chat Completions**

```ts
import { Router } from '@tokedex/sdk';

const router = new Router();

const completion = await router.chat.completions.create({
  model: 'openai/gpt-6-luna',
  messages: [{ role: 'user', content: 'Reply with exactly the word "hello".' }],
});
console.log(completion.choices[0]?.message.content);
```

```python
from tokedex import Router

with Router() as router:
    completion = router.chat.completions.create(
        model="openai/gpt-6-luna",
        messages=[{"role": "user", "content": 'Reply with exactly the word "hello".'}],
    )
    print(completion.choices[0].message.content)
```

```rust
use tokedex::{Router, wire};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let router = Router::builder().build()?;
    let messages = vec![wire::Message::user(r#"Reply with exactly the word "hello"."#)];
    let request = wire::ChatCompletionsRequest::new("openai/gpt-6-luna", messages);
    let completion = router.chat().completions().create(request).await?;
    if let Some(wire::Content::Text(text)) = &completion.choices[0].message.content {
        println!("{text}");
    }
    Ok(())
}
```

**Anthropic Messages**

```ts
import { Router } from '@tokedex/sdk';

const router = new Router();

const message = await router.messages.create({
  model: 'openai/gpt-6-luna',
  messages: [{ role: 'user', content: 'Reply with exactly the word "hello".' }],
});
for (const block of message.content) if (block.type === 'text') console.log(block.text);
```

```python
from tokedex import Router

with Router() as router:
    message = router.messages.create(
        model="openai/gpt-6-luna",
        messages=[{"role": "user", "content": 'Reply with exactly the word "hello".'}],
    )
    for block in message.content:
        if block.type == "text":
            print(block.text)
```

```rust
use tokedex::{Router, wire};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let router = Router::builder().build()?;
    let messages = vec![wire::Message::user(r#"Reply with exactly the word "hello"."#)];
    let request = wire::MessagesRequest::new("openai/gpt-6-luna", messages);
    let message = router.messages().create(request).await?;
    for block in &message.content {
        if let wire::ContentBlock::Text { text, .. } = block {
            println!("{text}");
        }
    }
    Ok(())
}
```

**Gemini GenerateContent**

```ts
import { Router } from '@tokedex/sdk';

const router = new Router();

const response = await router.generateContent.create({
  model: 'google/gemini-3.5-flash-lite',
  contents: [{ role: 'user', parts: [{ text: 'Reply with exactly the word "hello".' }] }],
});
for (const part of response.candidates?.[0]?.content?.parts ?? []) {
  if ('text' in part && !part.thought) console.log(part.text);
}
```

```python
from tokedex import Router
from tokedex.sdk.wire import GeminiTextPart

with Router() as router:
    response = router.generate_content.create(
        model="google/gemini-3.5-flash-lite",
        contents=[{"role": "user", "parts": [{"text": 'Reply with exactly the word "hello".'}]}],
    )
    for candidate in response.candidates or []:
        if candidate.content is None:
            continue
        for part in candidate.content.parts:
            if isinstance(part, GeminiTextPart) and not part.thought:
                print(part.text)
```

```rust
use serde_json::json;
use tokedex::{Router, wire};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let router = Router::builder().build()?;
    let request: wire::GeminiRequest = serde_json::from_value(json!({
        "contents": [{ "role": "user", "parts": [{ "text": "Reply with exactly the word \"hello\"." }] }],
    }))?;
    let response = router.generate_content().create("google/gemini-3.5-flash-lite", request).await?;
    for candidate in response.candidates.unwrap_or_default() {
        for part in candidate.content.map(|content| content.parts).unwrap_or_default() {
            if part["thought"].as_bool() != Some(true)
                && let Some(text) = part["text"].as_str()
            {
                println!("{text}");
            }
        }
    }
    Ok(())
}
```

Save it as `hello.ts` (Python: `hello.py`; Rust: `src/main.rs`). It sends one your API request and prints the reply's text.

In a larger program, create one Router when the program starts and reuse it for every call, so its connections are reused rather than opened for each call.

**OpenAI Responses**

The Router also holds the conversation turns it stores, which a new Router cannot find.

**Rust · Gemini GenerateContent**

Add `cargo add serde_json` to write the request as JSON.

## Run the program

```ts
npx tsx hello.ts
```

```python
uv run python hello.py
```

```rust
cargo run
```

```text
hello
```

The call uses your OpenAI (Gemini GenerateContent: Google) account and can incur charges.

## Call the Router from async code

**TypeScript, Rust**

Every call is already asynchronous: you `await` it.

**Python**

Use `AsyncRouter` in an `async with` block and `await` each call. It takes the same arguments as `Router`, and every other line stays the same. Avoid `Router` in async code: each of its calls blocks the thread until the reply arrives, which stalls your event loop.

Next: [SDK overview](/sdk), [Generate text](/sdk/generate-text) or [Run through a gateway](/sdk/gateway).
