TokenFly API

TokenFly is wire-compatible with the OpenAI and Anthropic APIs. If your code already uses one of those SDKs, switching means changing the base URL and the key.

Quickstart

  1. Create an account and add credit under Billing.
  2. Create a key under API keys. It starts with tf_live_ and is only shown once.
  3. Point your client at the base URL below.
{{origin}}/v1
from openai import OpenAI

client = OpenAI(base_url="{{origin}}/v1", api_key="tf_live_...")
r = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Say hi"}],
)
print(r.choices[0].message.content, r.usage.cost)

Authentication

Send your key as Authorization: Bearer tf_live_…. On the Messages endpoints, x-api-key is accepted as well, the way Anthropic SDKs send it. Keys are stored hashed. Revoking a key in the dashboard takes effect on the next request.

Endpoints

Method & pathPurpose
POST /v1/chat/completionsOpenAI Chat Completions, including tools, vision and streaming
POST /v1/responsesOpenAI Responses API (stateless)
POST /v1/completionsLegacy text completions
POST /v1/messagesAnthropic Messages
POST /v1/messages/count_tokensToken count estimate (free)
POST /v1/embeddingsText embeddings
POST /v1/rerank, /v2/rerankDocument reranking
POST /v1/images/generations, /v1/images/editsImage generation and editing
POST /v1/videos/generationsVideo generation (blocks until complete)
POST /v1/audio/speech, /transcriptions, /translationsText-to-speech and speech-to-text
POST /v1/uploads, DELETE /v1/uploads/{id}Temporary image uploads for vision prompts
GET /v1/modelsCatalog with TokenFly prices (no key needed)

Choosing a model

Pass any id from the catalog as model. Vendor-prefixed aliases such as anthropic/claude-opus-5 also resolve. Each catalog entry lists the endpoints it supports in supported_endpoints.

Streaming

Set "stream": true. Events are relayed as they arrive: chat.completion.chunk for Chat Completions, Anthropic's message_start … message_stop for Messages. Pass stream_options.include_usage to get a final usage chunk. Streams are metered whether or not you ask for it.

A stream can begin with HTTP 200 and fail partway. Always check the final event, and retry the whole request if the stream breaks.

Billing & usage

Requests are paid from your prepaid wallet at the rates in the catalog. Input and output token rates are never above the model maker's own list price. Non-streamed responses include the exact charge, in US dollars:

{
  "usage": { "prompt_tokens": 21, "completion_tokens": 9, "cost": 0.000012 },
  "request_id": "req_…",
  "billing": { "status": "settled", "billed_cost_usd": "0.000012", "currency": "USD" }
}

Each response also carries an x-tokenfly-request-id header that matches the entry under Usage in the dashboard. Streamed Chat Completions are charged from their token counts first, then corrected to the final settled figure a few seconds later.

Errors

Errors use the OpenAI envelope, {"error": {"message", "type", "code"}}, or the Anthropic one on /v1/messages.

StatusMeaning
400 / 422Invalid request, such as an unknown model or a malformed body
401Missing, invalid or revoked API key
402Wallet is empty. Add credit to continue.
429Rate or concurrency limit. Honour Retry-After.
502 / 503Provider capacity issue. Retry with exponential backoff.

Limits

Data handling

TokenFly stores request metadata needed for billing: time, endpoint, model, token counts, cost, latency and status. Prompts and completions are relayed in memory and never written to our database or logs.

TokenFly API 文档

TokenFly 与 OpenAI、Anthropic 的接口格式完全兼容。如果你的代码已经在用这两家的 SDK,只需更换 Base URL 和 API Key 即可切换。

快速开始

  1. 注册账号,并在「账单」中充值。
  2. 在「API Key」页面创建 Key。Key 以 tf_live_ 开头,只会显示一次。
  3. 把客户端的 Base URL 改为下面的地址。
{{origin}}/v1
from openai import OpenAI

client = OpenAI(base_url="{{origin}}/v1", api_key="tf_live_...")
r = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "你好"}],
)
print(r.choices[0].message.content, r.usage.cost)

身份验证

请求时带上 Authorization: Bearer tf_live_…。Messages 接口也接受 Anthropic SDK 使用的 x-api-key 请求头。Key 以哈希形式保存;在控制台吊销后,下一次请求即失效。

接口列表

方法与路径用途
POST /v1/chat/completionsOpenAI Chat Completions,支持工具调用、视觉输入与流式输出
POST /v1/responsesOpenAI Responses API(无状态)
POST /v1/completions传统文本补全
POST /v1/messagesAnthropic Messages
POST /v1/messages/count_tokens估算 token 数(免费)
POST /v1/embeddings文本向量
POST /v1/rerank、/v2/rerank文档重排序
POST /v1/images/generations、/v1/images/edits图像生成与编辑
POST /v1/videos/generations视频生成(等待生成完成后返回)
POST /v1/audio/speech、/transcriptions、/translations语音合成与语音识别
POST /v1/uploads、DELETE /v1/uploads/{id}为视觉输入临时上传图片
GET /v1/models模型列表与 TokenFly 价格(无需 Key)

选择模型

把模型列表中的任意 ID 填到 model 字段即可。带厂商前缀的别名(如 anthropic/claude-opus-5)同样可用。每个模型支持的接口列在 supported_endpoints 中。

流式输出

设置 "stream": true,事件会实时转发:Chat Completions 为 chat.completion.chunk,Messages 为 Anthropic 的 message_start … message_stop。传入 stream_options.include_usage 可在最后收到用量数据。无论是否传入,流式请求都会正常计费。

流式请求可能先返回 HTTP 200,再在中途出错。请务必检查最后一个事件;如果流中断,请重试整个请求。

计费与用量

费用从预付钱包中按模型列表中的单价扣除,输入与输出单价均不高于厂商官方价。非流式响应会附带本次请求的准确费用。API 返回的费用字段以美元计;控制台和价格页在中文界面下按当前汇率显示为人民币。

{
  "usage": { "prompt_tokens": 21, "completion_tokens": 9, "cost": 0.000012 },
  "request_id": "req_…",
  "billing": { "status": "settled", "billed_cost_usd": "0.000012", "currency": "USD" }
}

每个响应还带有 x-tokenfly-request-id 请求头,对应控制台「用量」中的记录。流式 Chat Completions 先按 token 数计费,几秒后再按最终结算金额校正。

错误码

错误采用 OpenAI 的格式 {"error": {"message", "type", "code"}};/v1/messages 接口采用 Anthropic 的格式。

状态码含义
400 / 422请求无效,例如模型不存在或请求体格式错误
401API Key 缺失、无效或已吊销
402钱包余额不足,充值后即可继续
429超出频率或并发限制,请遵循 Retry-After
502 / 503上游容量不足,请按指数退避重试

限制

数据处理

TokenFly 只保存计费所需的请求元数据:时间、接口、模型、token 数、费用、耗时和状态。提示词与回复内容只在内存中转发,从不写入数据库或日志。