Gapnil API documentation

Gapnil 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.
https://gapnil.com/v1
from openai import OpenAI

client = OpenAI(base_url="https://gapnil.com/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)

Authentication

Send your key as Authorization: Bearer tf_live_…. The x-api-key header 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, for example with rerank-v3.5
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 Gapnil 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. The last chunk before [DONE] has an empty choices array and carries the usage, whether or not you pass stream_options.include_usage.

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. Most non-streamed responses include the exact charge, in US dollars. The cost, request_id and billing fields are left out when the charge is not final yet, so read them as optional and use Usage in the dashboard for the settled figure:

{
  "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 a malformed body or a model called on an endpoint it does not support
401Missing, invalid or revoked API key
402Wallet is empty. Add credit to continue.
404Unknown model id or endpoint path
429Rate or concurrency limit. Honour Retry-After.
502 / 503Provider capacity issue. Retry with exponential backoff.

Limits

Data handling

Gapnil 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.

Gapnil API 开发文档与接入指南

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

快速开始

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

client = OpenAI(base_url="https://gapnil.com/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)

身份验证

请求时带上 Authorization: Bearer tf_live_…。也可以使用 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文档重排序,例如 rerank-v3.5
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模型列表与 Gapnil 价格(无需 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,[DONE] 之前的最后一个数据块都会带上用量,它的 choices 为空数组。流式请求同样正常计费。

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

计费与用量

费用从预付钱包中按模型列表中的单价扣除,输入与输出单价均不高于厂商官方价。多数非流式响应会附带本次请求的准确费用;费用尚未最终结算时,cost、request_id 和 billing 字段不会返回,请按可选字段处理,并以控制台「用量」中的结算金额为准。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钱包余额不足,充值后即可继续
404模型 ID 不存在,或接口路径错误
429超出频率或并发限制,请遵循 Retry-After
502 / 503上游容量不足,请按指数退避重试

限制

数据处理

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