
# SDK overview

Write a request once, in the API you already use, and send it to a model from any provider by changing its model ID.

**OpenAI Responses**

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

const router = new Router();

const response = await router.responses.create({
  model: 'openai/gpt-6-luna',
  input: 'Name the capital of France in one word.',
});
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="Name the capital of France in one word."
    )
    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", "Name the capital of France in one word.");
    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: 'Name the capital of France in one word.' }],
});
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": "Name the capital of France in one word."}],
    )
    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("Name the capital of France in one word.")];
    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: 'Name the capital of France in one word.' }],
});
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": "Name the capital of France in one word."}],
    )
    print("".join(block.text for block in message.content if block.type == "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("Name the capital of France in one word.")];
    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(())
}
```

```text
Paris
```

The model is OpenAI's, and the request and the reply are in your API. It needs `OPENAI_API_KEY` in your environment, and the call is billed by OpenAI.

**TypeScript**

Run it with `npx tsx hello.ts`.

## Behind the sample

The Router sent the request to OpenAI and gave you the reply in your API. `openai/gpt-6-luna` works the same in all three APIs, so the API you write in never decides the model. Your code reads every model's answer from the same fields and never sees a provider's format. To call another provider's model, change `model`: with `anthropic/claude-haiku-5.5`, the same request goes to Anthropic.

Most models are served on OpenAI Responses, OpenAI Chat Completions and Anthropic Messages. A few are served on one API only; `router.models.list()` (Rust: `router.models().list()`) returns one row for each model and API it serves.

The same Router also speaks a fourth API, Gemini GenerateContent, for Google's Gemini and Gemma models; see [Generate text](/sdk/generate-text).

Keep the API your code already uses, since most models are served on all three and nothing needs porting. For new code, start with OpenAI Responses: it is the only API with the typed `parse` (Rust: `parse()`) helper and with conversation turns the Router stores for you. Choose Gemini GenerateContent only for code that calls Google's models alone, because it serves no other provider's.

## Run the same code through a gateway

```ts
const router = new Router({ baseUrl: 'http://127.0.0.1:4000', apiKey: process.env.TOKEDEX_API_KEY! });
```

```python
import os

from tokedex import Router

with Router(base_url="http://127.0.0.1:4000", api_key=os.environ["TOKEDEX_API_KEY"]) as router:
    ...  # The same calls as before.
```

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

let router = Router::new(GatewayOptions::new("http://127.0.0.1:4000", std::env::var("TOKEDEX_API_KEY")?))?;
```

Change the line that creates the Router and keep every other line. The gateway then holds the provider credentials, so your process needs only a Tokedex API key, and each call appears in the gateway's dashboard under **Observability** and **Usage**. Through a gateway, you can call the models that gateway serves, and its administrator chooses them. [Run through a gateway](/sdk/gateway) covers headers and errors.

Move to a gateway once more than one application or person calls models on the same provider accounts: each then holds only its own API key, and you see every call in one dashboard.

## Other Router features

| To | Call | Guide |
|---|---|---|
| Stream the reply as it is written | `.stream(…)` on the same resource | [Stream a reply](/sdk/streaming) |
| Get a typed, validated value | `router.responses.parse` (Rust: `router.responses().parse`) | [Structured output](/sdk/structured-output) |
| Send images and files | Image and file parts in the request | [Images and files](/sdk/images-and-files) |
| Continue a conversation | The API's own history fields | [Conversations](/sdk/conversations) |
| Run a job at Batch prices | `router.batches.create` (Rust: `router.batches().create`) | [Batch jobs](/sdk/batch) |
| See which models and APIs you can call | `router.models.list()` (Rust: `router.models().list()`) | [Choose a model](/sdk/models) |

To choose each provider and account yourself, or to add hooks and sinks, build your own router with the core package; see [Routes and failover](/sdk/routes-and-failover).

## Install the SDK

Run `npm install @tokedex/sdk` (Python: `uv add tokedex`; Rust: `cargo add tokedex`), or follow the [SDK quickstart](/sdk/quickstart).

**Rust**

The samples also need Tokio: `cargo add tokio --features macros,rt-multi-thread`.

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