API Documentation

Learn how to integrate the Token Hub API into your applications

Quick Startt

Get started with Token Hub API in minutes. Our API is fully compatible with OpenAI's interface.

1

Get your API Key

Log in and generate your API key from the settings page.

2

Install SDK

Install the OpenAI SDK or use our compatible endpoint.

bash
pip install openai
3

Make your first request

Start sending requests with your preferred model.

Authentication

All API requests require authentication using your API key.

API Key Header

Include your API key in the Authorization header:

http
Authorization: Bearer YOUR_API_KEY

Chat Completions

Generate conversational responses using various AI models.

Endpoint

http
POST https://api.aitokenhub.io/v1/chat/completions

Request Parameters

model

Model to use ID

messages

Array of message objects

temperature

Sampling temperature (0-2)

max_tokens

Maximum number of tokens to generate

stream

Enable streaming responses

Example Request

typescript
import OpenAI from 'openai';

    // OpenAI  Base URL
    const client = new OpenAI({
      baseURL: 'https://api.aitokenhub.io/v1',
      apiKey: process.env.API_KEY,
    });

    // OpenRouter 
    // baseURL: 'https://api.aitokenhub.io/api/v1'

    const response = await client.chat.completions.create({
      model: 'gpt-4',
      messages: [
        { role: 'user', content: 'Hello!' }
      ],
    });

    console.log(response.choices[0].message.content);

Python Example Request

python
from openai import OpenAI

    # OpenAI  Base URL
    client = OpenAI(
      base_url="https://api.aitokenhub.io/v1",
      api_key="YOUR_API_KEY"
    )

    # OpenRouter 
    # base_url="https://api.aitokenhub.io/api/v1"

    response = client.chat.completions.create(
      model="gpt-4",
      messages=[
        {"role": "user", "content": "Hello!"}
      ]
    )

    print(response.choices[0].message.content)

Available Models

Access hundreds of AI models through a single API.

List Models Endpoint

http
GET https://api.aitokenhub.io/v1/models

Flagship Models

The latest, most powerful models from leading providers

Coding Specialist

Optimized for code generation and technical tasks

Reasoning Models

Advanced reasoning and complex problem solving

Multimodal

Supports image, audio, and video inputs

Streaming Responses

Real-time streaming responses for improved user experience.

Benefits of streaming:

  • Reduced perceived latency
  • Real-time feedback
  • Better user experience for long responses

Code Examples

typescript
const stream = await client.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: 'Tell me a story' }],
      stream: true,
    });

    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content || '');
    }

Anthropic Native API

Token Hub fully supports Anthropic's native /v1/messages API format. You can directly use the official Anthropic SDK with streaming and Prompt Cache support.

Base URL

Set the Anthropic SDK's base_url to the following address, using your Token Hub API Key:

http
Base URL: https://api.aitokenhub.io

Python SDK

python
import anthropic

    client = anthropic.Anthropic(
      base_url="https://api.aitokenhub.io",
      api_key="YOUR_API_KEY",
    )

    message = client.messages.create(
      model="claude-opus-4-6",
      max_tokens=1024,
      messages=[
        {"role": "user", "content": "Hello, Claude!"}
      ]
    )

    print(message.content[0].text)

TypeScript SDK

typescript
import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      baseURL: 'https://api.aitokenhub.io',
      apiKey: 'YOUR_API_KEY',
    });

    const message = await client.messages.create({
      model: 'claude-opus-4-6',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'Hello, Claude!' }
      ],
    });

    console.log(message.content[0].text);

Streaming

Use the Anthropic SDK's stream method for streaming output:

python
with client.messages.stream(
      model="claude-opus-4-6",
      max_tokens=1024,
      messages=[{"role": "user", "content": "Tell me a story"}]
    ) as stream:
      for text in stream.text_stream:
        print(text, end="", flush=True)

Prompt Cache

Use the cache_control parameter to enable prompt caching and reduce recurring token costs:

python
message = client.messages.create(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      system=[{
        "type": "text",
        "text": "You are a helpful assistant...(long system prompt)...",
        "cache_control": {"type": "ephemeral"}
      }],
      messages=[
        {"role": "user", "content": "Hello!"}
      ]
    )

    # Check cache usage
    print(f"Cache read: {message.usage.cache_read_input_tokens}")
    print(f"Cache creation: {message.usage.cache_creation_input_tokens}")

cURL Example

bash
curl https://api.aitokenhub.io/v1/messages \
      -H "x-api-key: YOUR_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "claude-opus-4-6",
        "max_tokens": 1024,
        "messages": [
          {"role": "user", "content": "Hello!"}
        ]
      }'

Supported Models

The following Claude models are currently available via the native API format:

Claude Opus 4

claude-opus-4-6

Claude Sonnet 4

claude-sonnet-4-6

Claude Haiku 3.5

claude-haiku-4-5

Error Handling

Effectively understand and handle API errors.

Common error codes.

  • 401 Unauthorized - Invalid API key
  • 429 Too Many Requests - Rate limit exceeded
  • 500 Internal Server Error - Service error
  • 503 Service Unavailable - Temporary downtime

Best Practices

  • Implement exponential backoff retries
  • Handle rate limits gracefully
  • Log errors for debugging

Pricing

Transparent pricing based on actual usage.

Model Input Price Output Price
GPT-4 $5.00 $15.00
GPT-3.5 Turbo $0.50 $1.50
Claude 3 Opus $15.00 $75.00

per 1M tokens

Pay as you go, no subscription required.

Track your usage and costs in real-time from the dashboard.

SDKs & Libraries

Official and community-maintained SDKs for popular languages.

Official SDKs

Python

Using the official OpenAI Python library

pip install openai

Node.js / TypeScript

Using the official OpenAI Node.js library

npm install openai

Popular Frameworks

  • LangChain: LangChain integration for building AI applications
  • Vercel AI SDK: Vercel AI SDK for React and Next.js applications

Rate Limits

API usage limits to ensure fair access and service stability.

Tier Requests Tokens
Free 100 req/day 100K tokens/day
Pro 10,000 req/day 10M tokens/day

>Rate limit information is included in response headers:

http
X-RateLimit-Limit: 10000
    X-RateLimit-Remaining: 9999
    X-RateLimit-Reset: 1640995200

Support Resources

Community

Join our Discord community for help and discussion

Email Support

Contact our team: support@Token Hub.dev

Status Page

Check real-time API status and uptime

Changelog

Stay updated on latest features and improvements