Language
API

SDK quickstart#

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

Install the SDK#

Use a supported Node.js LTS release (see engines in package.json) and an ESM project, with "type": "module" in package.json.

In your project directory, add the SDK:

npm install @tokedex/sdk
npm install --save-dev tsx
uv add tokedex
cargo add tokedex
cargo add tokio --features macros,rt-multi-thread

It needs Python 3.11 or later and uv.

It needs Rust 1.88 or later.

Set a provider key#

Shell
export OPENAI_API_KEY='<your-openai-api-key>'
Shell
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 lists every provider's variable.

Note:

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#

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);
}
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)
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(())
}
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);
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)
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(())
}
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);
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)
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(())
}
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);
}
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)
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.tshello.pysrc/main.rs. It sends one OpenAI ResponsesOpenAI Chat CompletionsAnthropic MessagesGemini GenerateContent 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.

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

Add cargo add serde_json to write the request as JSON.

Run the program#

npx tsx hello.ts
uv run python hello.py
cargo run
code
hello

The call uses your OpenAIGoogle account and can incur charges.

Call the Router from async code#

Every call is already asynchronous: you await it.

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, Generate text or Run through a gateway.