Create a chat completion
curl --request POST \
--url https://inference.magica.com/v1/chat/completions \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"messages": [
{}
],
"stream": true,
"max_tokens": 123,
"temperature": 123,
"tools": [
{}
],
"tool_choice": {}
}
'import requests
url = "https://inference.magica.com/v1/chat/completions"
payload = {
"model": "<string>",
"messages": [{}],
"stream": True,
"max_tokens": 123,
"temperature": 123,
"tools": [{}],
"tool_choice": {}
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
messages: [{}],
stream: true,
max_tokens: 123,
temperature: 123,
tools: [{}],
tool_choice: {}
})
};
fetch('https://inference.magica.com/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://inference.magica.com/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' => [
[
]
],
'stream' => true,
'max_tokens' => 123,
'temperature' => 123,
'tools' => [
[
]
],
'tool_choice' => [
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"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://inference.magica.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"max_tokens\": 123,\n \"temperature\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": {}\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
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://inference.magica.com/v1/chat/completions")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"max_tokens\": 123,\n \"temperature\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": {}\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://inference.magica.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"max_tokens\": 123,\n \"temperature\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": {}\n}"
response = http.request(request)
puts response.read_bodyLLM Gateway
Create a chat completion
Generate a complete JSON chat response or stream it as server-sent events.
POST
/
v1
/
chat
/
completions
Create a chat completion
curl --request POST \
--url https://inference.magica.com/v1/chat/completions \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"messages": [
{}
],
"stream": true,
"max_tokens": 123,
"temperature": 123,
"tools": [
{}
],
"tool_choice": {}
}
'import requests
url = "https://inference.magica.com/v1/chat/completions"
payload = {
"model": "<string>",
"messages": [{}],
"stream": True,
"max_tokens": 123,
"temperature": 123,
"tools": [{}],
"tool_choice": {}
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
messages: [{}],
stream: true,
max_tokens: 123,
temperature: 123,
tools: [{}],
tool_choice: {}
})
};
fetch('https://inference.magica.com/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://inference.magica.com/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' => [
[
]
],
'stream' => true,
'max_tokens' => 123,
'temperature' => 123,
'tools' => [
[
]
],
'tool_choice' => [
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"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://inference.magica.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"max_tokens\": 123,\n \"temperature\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": {}\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
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://inference.magica.com/v1/chat/completions")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"max_tokens\": 123,\n \"temperature\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": {}\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://inference.magica.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"max_tokens\": 123,\n \"temperature\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": {}\n}"
response = http.request(request)
puts response.read_bodyCreates a response from a conversation represented as a
messages array. Use
this endpoint for chat history, tool calls, images, and PDFs.
Authorization
Body
Model ID from the language-model catalog.
Conversation messages with
developer, system, user, assistant, or
tool roles. Send previous turns again when the next answer depends on them.Omit or set
false for one complete JSON object. Set true for server-sent
events.Maximum output tokens. Must be at least
1 and no greater than the model’s
advertised output limit.Controls whether the model may call a tool, must call one, or must call a
named function.
Direct JSON request
- cURL
- Node.js
- Python
curl https://inference.magica.com/v1/chat/completions \
-H "Authorization: Bearer $MAGICA_API_KEY" \
-H "Content-Type: application/json" \
--data '{
"model": "openai/gpt-5.4-mini",
"messages": [{"role": "user", "content": "Reply with exactly: hello"}]
}'
const response = await fetch(
"https://inference.magica.com/v1/chat/completions",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.MAGICA_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/gpt-5.4-mini",
messages: [{ role: "user", content: "Reply with exactly: hello" }],
}),
},
);
if (!response.ok) throw new Error(await response.text());
const completion = await response.json();
console.log(completion.choices[0].message.content);
import os, requests
response = requests.post(
"https://inference.magica.com/v1/chat/completions",
headers={"Authorization": f"Bearer {os.environ['MAGICA_API_KEY']}"},
json={
"model": "openai/gpt-5.4-mini",
"messages": [{"role": "user", "content": "Reply with exactly: hello"}],
},
timeout=120,
)
response.raise_for_status()
print(response.json()["choices"][0]["message"]["content"])
Direct JSON response
{
"id": "chatcmpl_123",
"object": "chat.completion",
"model": "openai/gpt-5.4-mini",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "hello" },
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 1,
"total_tokens": 21
}
}
Stream the response
Add"stream": true and read each SSE data: frame. The final usage chunk is
followed by data: [DONE].
data: {"id":"chatcmpl_123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"hello"},"finish_reason":null}]}
data: {"id":"chatcmpl_123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":20,"completion_tokens":1,"total_tokens":21}}
data: [DONE]
Images and PDFs
Inside a user message, use:{"type":"image_url","image_url":{"url":"https://..."}}for an image.{"type":"file","file":{"filename":"report.pdf","file_data":"https://..."}}for a PDF.
Errors
| Status | Reason |
|---|---|
400 | Invalid JSON, messages, tool input, or output limit |
401 | Missing or invalid API key |
402 | Insufficient credits |
403 | Access token lacks the llm:inference scope |
404 | Model ID was not found |
413 | Request exceeds the direct transport or selected model limit |
429 | Rate limit reached; retry using Retry-After |
502 | Model service could not complete the request |
503 | Credit authorization is temporarily unavailable |
529 | Model service is temporarily overloaded |
Was this page helpful?