Web / HTTP Basics
Tokenfly's RESTful API is fully compatible with the OpenAI API format
TokenFly provides a RESTful API that is fully compatible with the OpenAI API format. For web clients or any client capable of sending HTTP requests, you simply need to point your request URL at the TokenFly service address and pass the appropriate authentication credentials in the header.
Prerequisites
Make sure every HTTP request includes the following:
- Request URL: point to the specific TokenFly endpoint (e.g.
https://tokenfly.com/v1/...). - Headers:
Content-Type: application/json: declares the request body format to the server.Authorization: Bearer <YOUR_Tokenfly_KEY>: your authorization key.
- Body: all request data should be submitted in JSON format.
Example: Chat Completions Request (/chat/completions)
Using the chat completions endpoint as an example, here's how to make a real HTTP request, including a basic request, the response format, streaming output, and function calling. This endpoint supports passing custom parameters via metadata.
Basic Chat Request
HTTP / cURL example:
curl https://tokenfly.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $YOUR_Tokenfly_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
"metadata": {
"tags": ["production", "customer-support", "urgent"],
"generation_name": "support-bot",
"trace_user_id": "user-123"
}
}'Web fetch example:
fetch("https://tokenfly.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $YOUR_Tokenfly_KEY"
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{ role: "user", content: "hello!" }
],
metadata: { tags: ["web-client"] }
})
}).then(res => res.json()).then(console.log);Response Format Reference
On success, you'll receive a JSON object similar to the following:
{
"id": "chatcmpl-8c5qbGTILZa1S4CK3b31yj5N40hFN",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "As an AI language model...",
"role": "assistant"
}
}
],
"created": 1704089632,
"model": "gpt-35-turbo",
"object": "chat.completion",
"usage": {
"completion_tokens": 47,
"prompt_tokens": 12,
"total_tokens": 59
},
"_response_ms": 1753.426
}Enabling Streaming
To implement a "typewriter" style real-time response on the web, pass "stream": true in the request payload.
HTTP / cURL example:
curl https://tokenfly.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $YOUR_Tokenfly_KEY" \
-d '{
"model": "gpt-4-turbo",
"messages": [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
"stream": true
}'Function Calling
You can pass function definitions to the model over HTTP, and the model will generate the corresponding tool call arguments for you.
HTTP / cURL example:
curl https://tokenfly.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $YOUR_Tokenfly_KEY" \
-d '{
"model": "gpt-4-turbo",
"messages": [
{
"role": "user",
"content": "What'\''s the weather like in Boston today?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}'How is this guide?