Create a chat completion
curl --request POST \
--url https://tokens.flex.ai/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "Meta-Llama-3.1-8B-Instruct-FP8",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_calls": [
{}
],
"tool_call_id": "<string>"
}
],
"stream": false,
"stream_options": {
"include_usage": true
},
"temperature": 1,
"top_p": 0.5,
"max_tokens": 2,
"tools": [
{
"type": "function",
"function": {
"name": "<string>",
"description": "<string>",
"parameters": {}
}
}
],
"response_format": {},
"seed": 123,
"stop": "<string>",
"user": "<string>"
}
'import requests
url = "https://tokens.flex.ai/v1/chat/completions"
payload = {
"model": "Meta-Llama-3.1-8B-Instruct-FP8",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_calls": [{}],
"tool_call_id": "<string>"
}
],
"stream": False,
"stream_options": { "include_usage": True },
"temperature": 1,
"top_p": 0.5,
"max_tokens": 2,
"tools": [
{
"type": "function",
"function": {
"name": "<string>",
"description": "<string>",
"parameters": {}
}
}
],
"response_format": {},
"seed": 123,
"stop": "<string>",
"user": "<string>"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'Meta-Llama-3.1-8B-Instruct-FP8',
messages: [
{
content: '<string>',
name: '<string>',
tool_calls: [{}],
tool_call_id: '<string>'
}
],
stream: false,
stream_options: {include_usage: true},
temperature: 1,
top_p: 0.5,
max_tokens: 2,
tools: [
{
type: 'function',
function: {name: '<string>', description: '<string>', parameters: {}}
}
],
response_format: {},
seed: 123,
stop: '<string>',
user: '<string>'
})
};
fetch('https://tokens.flex.ai/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://tokens.flex.ai/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'Meta-Llama-3.1-8B-Instruct-FP8',
'messages' => [
[
'content' => '<string>',
'name' => '<string>',
'tool_calls' => [
[
]
],
'tool_call_id' => '<string>'
]
],
'stream' => false,
'stream_options' => [
'include_usage' => true
],
'temperature' => 1,
'top_p' => 0.5,
'max_tokens' => 2,
'tools' => [
[
'type' => 'function',
'function' => [
'name' => '<string>',
'description' => '<string>',
'parameters' => [
]
]
]
],
'response_format' => [
],
'seed' => 123,
'stop' => '<string>',
'user' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://tokens.flex.ai/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"Meta-Llama-3.1-8B-Instruct-FP8\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_calls\": [\n {}\n ],\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"stream_options\": {\n \"include_usage\": true\n },\n \"temperature\": 1,\n \"top_p\": 0.5,\n \"max_tokens\": 2,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {}\n }\n }\n ],\n \"response_format\": {},\n \"seed\": 123,\n \"stop\": \"<string>\",\n \"user\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://tokens.flex.ai/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"Meta-Llama-3.1-8B-Instruct-FP8\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_calls\": [\n {}\n ],\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"stream_options\": {\n \"include_usage\": true\n },\n \"temperature\": 1,\n \"top_p\": 0.5,\n \"max_tokens\": 2,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {}\n }\n }\n ],\n \"response_format\": {},\n \"seed\": 123,\n \"stop\": \"<string>\",\n \"user\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://tokens.flex.ai/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"Meta-Llama-3.1-8B-Instruct-FP8\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_calls\": [\n {}\n ],\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"stream_options\": {\n \"include_usage\": true\n },\n \"temperature\": 1,\n \"top_p\": 0.5,\n \"max_tokens\": 2,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {}\n }\n }\n ],\n \"response_format\": {},\n \"seed\": 123,\n \"stop\": \"<string>\",\n \"user\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"id": "<string>",
"object": "chat.completion",
"created": 123,
"model": "<string>",
"choices": [
{
"index": 123,
"message": {
"role": "<string>",
"content": "<string>",
"tool_calls": [
{}
]
}
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 123,
"total_tokens": 123,
"cache_read_input_tokens": 123,
"cache_creation_input_tokens": 123
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}Chat
Create a chat completion
Given a list of messages, returns a model-generated response. Supports streaming (SSE), tool use, and (on some models) vision input.
Streaming: set stream: true and stream_options: { include_usage: true }
to receive token-by-token deltas. The final chunk (the one with finish_reason
set) carries the usage block; you need it for accurate cost tracking.
POST
/
v1
/
chat
/
completions
Create a chat completion
curl --request POST \
--url https://tokens.flex.ai/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "Meta-Llama-3.1-8B-Instruct-FP8",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_calls": [
{}
],
"tool_call_id": "<string>"
}
],
"stream": false,
"stream_options": {
"include_usage": true
},
"temperature": 1,
"top_p": 0.5,
"max_tokens": 2,
"tools": [
{
"type": "function",
"function": {
"name": "<string>",
"description": "<string>",
"parameters": {}
}
}
],
"response_format": {},
"seed": 123,
"stop": "<string>",
"user": "<string>"
}
'import requests
url = "https://tokens.flex.ai/v1/chat/completions"
payload = {
"model": "Meta-Llama-3.1-8B-Instruct-FP8",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_calls": [{}],
"tool_call_id": "<string>"
}
],
"stream": False,
"stream_options": { "include_usage": True },
"temperature": 1,
"top_p": 0.5,
"max_tokens": 2,
"tools": [
{
"type": "function",
"function": {
"name": "<string>",
"description": "<string>",
"parameters": {}
}
}
],
"response_format": {},
"seed": 123,
"stop": "<string>",
"user": "<string>"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'Meta-Llama-3.1-8B-Instruct-FP8',
messages: [
{
content: '<string>',
name: '<string>',
tool_calls: [{}],
tool_call_id: '<string>'
}
],
stream: false,
stream_options: {include_usage: true},
temperature: 1,
top_p: 0.5,
max_tokens: 2,
tools: [
{
type: 'function',
function: {name: '<string>', description: '<string>', parameters: {}}
}
],
response_format: {},
seed: 123,
stop: '<string>',
user: '<string>'
})
};
fetch('https://tokens.flex.ai/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://tokens.flex.ai/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'Meta-Llama-3.1-8B-Instruct-FP8',
'messages' => [
[
'content' => '<string>',
'name' => '<string>',
'tool_calls' => [
[
]
],
'tool_call_id' => '<string>'
]
],
'stream' => false,
'stream_options' => [
'include_usage' => true
],
'temperature' => 1,
'top_p' => 0.5,
'max_tokens' => 2,
'tools' => [
[
'type' => 'function',
'function' => [
'name' => '<string>',
'description' => '<string>',
'parameters' => [
]
]
]
],
'response_format' => [
],
'seed' => 123,
'stop' => '<string>',
'user' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://tokens.flex.ai/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"Meta-Llama-3.1-8B-Instruct-FP8\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_calls\": [\n {}\n ],\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"stream_options\": {\n \"include_usage\": true\n },\n \"temperature\": 1,\n \"top_p\": 0.5,\n \"max_tokens\": 2,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {}\n }\n }\n ],\n \"response_format\": {},\n \"seed\": 123,\n \"stop\": \"<string>\",\n \"user\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://tokens.flex.ai/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"Meta-Llama-3.1-8B-Instruct-FP8\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_calls\": [\n {}\n ],\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"stream_options\": {\n \"include_usage\": true\n },\n \"temperature\": 1,\n \"top_p\": 0.5,\n \"max_tokens\": 2,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {}\n }\n }\n ],\n \"response_format\": {},\n \"seed\": 123,\n \"stop\": \"<string>\",\n \"user\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://tokens.flex.ai/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"Meta-Llama-3.1-8B-Instruct-FP8\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_calls\": [\n {}\n ],\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"stream_options\": {\n \"include_usage\": true\n },\n \"temperature\": 1,\n \"top_p\": 0.5,\n \"max_tokens\": 2,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {}\n }\n }\n ],\n \"response_format\": {},\n \"seed\": 123,\n \"stop\": \"<string>\",\n \"user\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"id": "<string>",
"object": "chat.completion",
"created": 123,
"model": "<string>",
"choices": [
{
"index": 123,
"message": {
"role": "<string>",
"content": "<string>",
"tool_calls": [
{}
]
}
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 123,
"total_tokens": 123,
"cache_read_input_tokens": 123,
"cache_creation_input_tokens": 123
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>",
"doc_url": "<string>"
}
}Authorizations
Virtual API key. Create one from the
FlexAI dashboard. Pass as
Authorization: Bearer sk-xxxx.
Body
application/json
Model id. See GET /v1/models for available models.
Example:
"Meta-Llama-3.1-8B-Instruct-FP8"
Show child attributes
Show child attributes
Show child attributes
Show child attributes
Required range:
0 <= x <= 2Required range:
0 <= x <= 1Required range:
x >= 1Show child attributes
Show child attributes
Available options:
none, auto, required Show child attributes
Show child attributes
Was this page helpful?
⌘I