curl --request POST \
--url https://api.swarms.world/v1/chat/completions \
--header 'Content-Type: application/json' \
--header 'x-api-key: <x-api-key>' \
--data '
{
"model": "<string>",
"messages": [
{
"role": "<string>",
"content": "<string>",
"name": "<string>"
}
],
"temperature": 0.5,
"top_p": 123,
"n": 1,
"stream": false,
"max_tokens": 123,
"max_completion_tokens": 123,
"presence_penalty": 123,
"frequency_penalty": 123,
"user": "<string>",
"max_loops": 1
}
'import requests
url = "https://api.swarms.world/v1/chat/completions"
payload = {
"model": "<string>",
"messages": [
{
"role": "<string>",
"content": "<string>",
"name": "<string>"
}
],
"temperature": 0.5,
"top_p": 123,
"n": 1,
"stream": False,
"max_tokens": 123,
"max_completion_tokens": 123,
"presence_penalty": 123,
"frequency_penalty": 123,
"user": "<string>",
"max_loops": 1
}
headers = {
"x-api-key": "<x-api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-api-key': '<x-api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
messages: [{role: '<string>', content: '<string>', name: '<string>'}],
temperature: 0.5,
top_p: 123,
n: 1,
stream: false,
max_tokens: 123,
max_completion_tokens: 123,
presence_penalty: 123,
frequency_penalty: 123,
user: '<string>',
max_loops: 1
})
};
fetch('https://api.swarms.world/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://api.swarms.world/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' => '<string>',
'messages' => [
[
'role' => '<string>',
'content' => '<string>',
'name' => '<string>'
]
],
'temperature' => 0.5,
'top_p' => 123,
'n' => 1,
'stream' => false,
'max_tokens' => 123,
'max_completion_tokens' => 123,
'presence_penalty' => 123,
'frequency_penalty' => 123,
'user' => '<string>',
'max_loops' => 1
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <x-api-key>"
],
]);
$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://api.swarms.world/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"role\": \"<string>\",\n \"content\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"temperature\": 0.5,\n \"top_p\": 123,\n \"n\": 1,\n \"stream\": false,\n \"max_tokens\": 123,\n \"max_completion_tokens\": 123,\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"user\": \"<string>\",\n \"max_loops\": 1\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<x-api-key>")
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://api.swarms.world/v1/chat/completions")
.header("x-api-key", "<x-api-key>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"role\": \"<string>\",\n \"content\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"temperature\": 0.5,\n \"top_p\": 123,\n \"n\": 1,\n \"stream\": false,\n \"max_tokens\": 123,\n \"max_completion_tokens\": 123,\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"user\": \"<string>\",\n \"max_loops\": 1\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.swarms.world/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<x-api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"role\": \"<string>\",\n \"content\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"temperature\": 0.5,\n \"top_p\": 123,\n \"n\": 1,\n \"stream\": false,\n \"max_tokens\": 123,\n \"max_completion_tokens\": 123,\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"user\": \"<string>\",\n \"max_loops\": 1\n}"
response = http.request(request)
puts response.read_body{
"id": "<string>",
"created": 123,
"model": "<string>",
"choices": [
{
"message": {
"role": "<string>",
"content": "<string>",
"name": "<string>"
},
"index": 0,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
"object": "chat.completion"
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>",
"input": "<unknown>",
"ctx": {}
}
]
}Chat Completions (OpenAI-compatible)
OpenAI-compatible chat completions endpoint. Accepts the standard OpenAI request schema and returns the standard OpenAI response schema. Supports both streaming (SSE) and non-streaming modes. Works as a drop-in replacement with the OpenAI Python and TypeScript SDKs.
curl --request POST \
--url https://api.swarms.world/v1/chat/completions \
--header 'Content-Type: application/json' \
--header 'x-api-key: <x-api-key>' \
--data '
{
"model": "<string>",
"messages": [
{
"role": "<string>",
"content": "<string>",
"name": "<string>"
}
],
"temperature": 0.5,
"top_p": 123,
"n": 1,
"stream": false,
"max_tokens": 123,
"max_completion_tokens": 123,
"presence_penalty": 123,
"frequency_penalty": 123,
"user": "<string>",
"max_loops": 1
}
'import requests
url = "https://api.swarms.world/v1/chat/completions"
payload = {
"model": "<string>",
"messages": [
{
"role": "<string>",
"content": "<string>",
"name": "<string>"
}
],
"temperature": 0.5,
"top_p": 123,
"n": 1,
"stream": False,
"max_tokens": 123,
"max_completion_tokens": 123,
"presence_penalty": 123,
"frequency_penalty": 123,
"user": "<string>",
"max_loops": 1
}
headers = {
"x-api-key": "<x-api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-api-key': '<x-api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
messages: [{role: '<string>', content: '<string>', name: '<string>'}],
temperature: 0.5,
top_p: 123,
n: 1,
stream: false,
max_tokens: 123,
max_completion_tokens: 123,
presence_penalty: 123,
frequency_penalty: 123,
user: '<string>',
max_loops: 1
})
};
fetch('https://api.swarms.world/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://api.swarms.world/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' => '<string>',
'messages' => [
[
'role' => '<string>',
'content' => '<string>',
'name' => '<string>'
]
],
'temperature' => 0.5,
'top_p' => 123,
'n' => 1,
'stream' => false,
'max_tokens' => 123,
'max_completion_tokens' => 123,
'presence_penalty' => 123,
'frequency_penalty' => 123,
'user' => '<string>',
'max_loops' => 1
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <x-api-key>"
],
]);
$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://api.swarms.world/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"role\": \"<string>\",\n \"content\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"temperature\": 0.5,\n \"top_p\": 123,\n \"n\": 1,\n \"stream\": false,\n \"max_tokens\": 123,\n \"max_completion_tokens\": 123,\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"user\": \"<string>\",\n \"max_loops\": 1\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<x-api-key>")
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://api.swarms.world/v1/chat/completions")
.header("x-api-key", "<x-api-key>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"role\": \"<string>\",\n \"content\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"temperature\": 0.5,\n \"top_p\": 123,\n \"n\": 1,\n \"stream\": false,\n \"max_tokens\": 123,\n \"max_completion_tokens\": 123,\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"user\": \"<string>\",\n \"max_loops\": 1\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.swarms.world/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<x-api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"role\": \"<string>\",\n \"content\": \"<string>\",\n \"name\": \"<string>\"\n }\n ],\n \"temperature\": 0.5,\n \"top_p\": 123,\n \"n\": 1,\n \"stream\": false,\n \"max_tokens\": 123,\n \"max_completion_tokens\": 123,\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"user\": \"<string>\",\n \"max_loops\": 1\n}"
response = http.request(request)
puts response.read_body{
"id": "<string>",
"created": 123,
"model": "<string>",
"choices": [
{
"message": {
"role": "<string>",
"content": "<string>",
"name": "<string>"
},
"index": 0,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
"object": "chat.completion"
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>",
"input": "<unknown>",
"ctx": {}
}
]
}Headers
Body
Model to use for completion
A list of messages comprising the conversation
Show child attributes
Show child attributes
Sampling temperature (0 to 2)
0 <= x <= 2Nucleus sampling parameter
Number of completions to generate
Whether to stream partial results
Maximum tokens to generate
Maximum completion tokens (takes precedence over max_tokens)
Presence penalty
Frequency penalty
Unique identifier for the end-user
Maximum number of agent reasoning loops (Swarms extension). Defaults to 1 (single pass). Use via extra_body in the OpenAI SDK.
Was this page helpful?