Create a chat completion (OpenAI-compatible)
Chat with a language model. This endpoint is consistent with the OpenAI Chat Completions API and may be used with the OpenAI JS or Python SDK by setting the base URL to https://api.telnyx.com/v2/ai/openai.
import Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});
const response = await client.ai.openai.chat.createCompletion({
messages: [
{ role: 'system', content: 'You are a friendly chatbot.' },
{ role: 'user', content: 'Hello, world!' },
],
});
console.log(response);import os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)
response = client.ai.openai.chat.create_completion(
messages=[{
"role": "system",
"content": "You are a friendly chatbot.",
}, {
"role": "user",
"content": "Hello, world!",
}],
)
print(response)
package main
import (
"context"
"fmt"
"github.com/team-telnyx/telnyx-go"
"github.com/team-telnyx/telnyx-go/option"
)
func main() {
client := telnyx.NewClient(
option.WithAPIKey("My API Key"),
)
response, err := client.AI.OpenAI.Chat.NewCompletion(context.TODO(), telnyx.AIOpenAIChatNewCompletionParams{
Messages: []telnyx.AIOpenAIChatNewCompletionParamsMessage{{
Role: "system",
Content: telnyx.AIOpenAIChatNewCompletionParamsMessageContentUnion{
OfString: telnyx.String("You are a friendly chatbot."),
},
}, {
Role: "user",
Content: telnyx.AIOpenAIChatNewCompletionParamsMessageContentUnion{
OfString: telnyx.String("Hello, world!"),
},
}},
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", response)
}
package com.telnyx.sdk.example;
import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
import com.telnyx.sdk.models.ai.openai.chat.ChatCreateCompletionParams;
import com.telnyx.sdk.models.ai.openai.chat.ChatCreateCompletionResponse;
public final class Main {
private Main() {}
public static void main(String[] args) {
TelnyxClient client = TelnyxOkHttpClient.fromEnv();
ChatCreateCompletionParams params = ChatCreateCompletionParams.builder()
.addMessage(ChatCreateCompletionParams.Message.builder()
.content("You are a friendly chatbot.")
.role(ChatCreateCompletionParams.Message.Role.SYSTEM)
.build())
.addMessage(ChatCreateCompletionParams.Message.builder()
.content("Hello, world!")
.role(ChatCreateCompletionParams.Message.Role.USER)
.build())
.build();
ChatCreateCompletionResponse response = client.ai().openai().chat().createCompletion(params);
}
}require "telnyx"
telnyx = Telnyx::Client.new(api_key: "My API Key")
response = telnyx.ai.openai.chat.create_completion(
messages: [{content: "You are a friendly chatbot.", role: :system}, {content: "Hello, world!", role: :user}]
)
puts(response)<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
use Telnyx\Client;
use Telnyx\Core\Exceptions\APIException;
$client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API Key');
try {
$response = $client->ai->openai->chat->createCompletion(
messages: [
['content' => 'You are a friendly chatbot.', 'role' => 'system'],
['content' => 'Hello, world!', 'role' => 'user'],
],
apiKeyRef: 'api_key_ref',
bestOf: 0,
earlyStopping: true,
enableThinking: true,
frequencyPenalty: 0,
guidedChoice: ['string'],
guidedJson: ['foo' => 'bar'],
guidedRegex: 'guided_regex',
lengthPenalty: 0,
logprobs: true,
maxTokens: 0,
minP: 0,
model: 'model',
n: 0,
presencePenalty: 0,
responseFormat: ['type' => 'text'],
seed: 0,
stop: 'string',
stream: true,
temperature: 0,
toolChoice: 'none',
tools: [
[
'function' => [
'name' => 'name',
'description' => 'description',
'parameters' => ['foo' => 'bar'],
],
'type' => 'function',
],
],
topLogprobs: 0,
topP: 0,
useBeamSearch: true,
);
var_dump($response);
} catch (APIException $e) {
echo $e->getMessage();
}telnyx ai:openai:chat create-completion \
--api-key 'My API Key' \
--message '{content: You are a friendly chatbot., role: system}' \
--message "{content: 'Hello, world!', role: user}"curl --request POST \
--url https://api.telnyx.com/v2/ai/openai/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"messages": [
{
"role": "system",
"content": "You are a friendly chatbot."
},
{
"role": "user",
"content": "Hello, world!"
}
]
}
'{
"id": "chatcmpl-8b1f2a3c9d4e5f6a",
"object": "chat.completion",
"created": 1706011802,
"model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Telnyx provides carrier-grade voice, messaging, and AI inference through one API."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 24,
"completion_tokens": 18,
"total_tokens": 42
}
}{
"detail": [
{
"loc": [
"body",
"name"
],
"msg": "Field required",
"type": "missing"
}
]
}Authorizations
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
Body
A list of the previous chat messages for context.
Show child attributes
Show child attributes
[
{
"role": "system",
"content": "You are a friendly chatbot."
},
{
"role": "user",
"content": "Hello, world!"
}
]
The language model to chat with.
The service tier to use for this request. Supported values vary by model; use GET /v2/ai/openai/models and inspect the model's service_tiers field. If omitted, Telnyx-hosted models use default.
Optional data-residency region the request should be served from, using the same vocabulary as your account's Data Locality setting. Behavior depends on mode. Supported for Telnyx-hosted models only: a request routed to an external provider never passes through Telnyx model routing, so a region cannot be enforced for it. Omit for today's latency-based routing.
USA, EU, AUS, UAE How strictly region is applied. preferred (the default when region is set) tries that region first and falls back to another when the model cannot be served there, so a request that would have succeeded still succeeds. strict pins the request: it is served from that region or it fails with a 422, never redirected to another region. Requires region.
preferred, strict If you are using an external inference provider like xAI or OpenAI, this field allows you to pass along a reference to your API key. After creating an integration secret for you API key, pass the secret's identifier in this field.
Whether or not to stream data-only server-sent events as they become available.
Adjusts the "creativity" of the model. Lower values make the model more deterministic and repetitive, while higher values make the model more random and creative.
Maximum number of completion (output) tokens, including reasoning and answer tokens combined. An omitted or null limit defaults to 8192 unless the model is excluded from that default; excluded models leave the limit to the serving engine. Explicit values and applied defaults are clamped to the model's max_completion_tokens metadata when that ceiling is set (see GET /v2/ai/openai/models). An omitted limit on an excluded model remains unset even when metadata declares a ceiling. Input and output together must fit the model's context window. Set an explicit limit when a bounded generation budget is required. For current Telnyx-hosted models, omitted or null limits default to 8192 for zai-org/GLM-5.3, zai-org/GLM-5.3-Flash and deepseek-ai/DeepSeek-V4.1-Flash. The hosted exclusions are moonshotai/Kimi-K3, moonshotai/Kimi-K2.6, moonshotai/Kimi-K2.5, zai-org/GLM-5.2, zai-org/GLM-5.1-FP8, MiniMaxAI/MiniMax-M3-MXFP8, MiniMaxAI/MiniMax-M2.7, Qwen/Qwen3.8-27B, Qwen/Qwen3-235B-A22B, deepseek-ai/DeepSeek-V4-Flash-0731, meta-llama/Llama-3.3-70B-Instruct, meta-llama/Meta-Llama-3.1-70B-Instruct, meta-llama/Meta-Llama-3.1-8B-Instruct and google/gemma-2b-it. Legacy external and fine-tuned model IDs can also bypass the default; model discovery reports completion ceilings, not whether this default applies. Set an explicit limit for predictable behavior across models.
The function tool type follows the same schema as the OpenAI Chat Completions API. The retrieval tool type is unique to Telnyx. You may pass a list of embedded storage buckets for retrieval-augmented generation.
- Function
- Retrieval
Show child attributes
Show child attributes
none, auto, required Plain text output.
- ResponseFormatText
- ResponseFormatJsonObject
- ResponseFormatJsonSchemaParam
Show child attributes
Show child attributes
This is an alternative to top_p that many prefer. Must be in [0, 1].
This will return multiple choices for you instead of a single chat completion.
Setting this to true will allow the model to explore more completion options. This is not supported by OpenAI.
This is used with use_beam_search to determine how many candidate beams to explore.
This is used with use_beam_search to prefer shorter or longer completions.
This is used with use_beam_search. If true, generation stops as soon as there are best_of complete candidates; if false, a heuristic is applied and the generation stops when is it very unlikely to find better candidates.
Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message.
This is used with logprobs. An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
Higher values will penalize the model from repeating the same output tokens.
Higher values will penalize the model from repeating the same output tokens.
An alternative or complement to temperature. This adjusts how many of the top possibilities to consider.
Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.
Whether to enable the thinking/reasoning phase for models that support it (e.g., QwQ, Qwen3). When set to false, the model will skip the internal reasoning step and respond directly, which can reduce latency. Defaults to true.
Controls the reasoning effort for models that support it. When set, the model spends more or less compute on internal reasoning before generating its response. Supported values: none, minimal, low, medium, high, xhigh, max. Not all models support all values; unsupported values are rejected with a 400 error. When omitted, reasoning models use their default effort level.
none, minimal, low, medium, high, xhigh, max Response
Successful Response
The response is of type object.
Was this page helpful?
import Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});
const response = await client.ai.openai.chat.createCompletion({
messages: [
{ role: 'system', content: 'You are a friendly chatbot.' },
{ role: 'user', content: 'Hello, world!' },
],
});
console.log(response);import os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)
response = client.ai.openai.chat.create_completion(
messages=[{
"role": "system",
"content": "You are a friendly chatbot.",
}, {
"role": "user",
"content": "Hello, world!",
}],
)
print(response)
package main
import (
"context"
"fmt"
"github.com/team-telnyx/telnyx-go"
"github.com/team-telnyx/telnyx-go/option"
)
func main() {
client := telnyx.NewClient(
option.WithAPIKey("My API Key"),
)
response, err := client.AI.OpenAI.Chat.NewCompletion(context.TODO(), telnyx.AIOpenAIChatNewCompletionParams{
Messages: []telnyx.AIOpenAIChatNewCompletionParamsMessage{{
Role: "system",
Content: telnyx.AIOpenAIChatNewCompletionParamsMessageContentUnion{
OfString: telnyx.String("You are a friendly chatbot."),
},
}, {
Role: "user",
Content: telnyx.AIOpenAIChatNewCompletionParamsMessageContentUnion{
OfString: telnyx.String("Hello, world!"),
},
}},
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", response)
}
package com.telnyx.sdk.example;
import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
import com.telnyx.sdk.models.ai.openai.chat.ChatCreateCompletionParams;
import com.telnyx.sdk.models.ai.openai.chat.ChatCreateCompletionResponse;
public final class Main {
private Main() {}
public static void main(String[] args) {
TelnyxClient client = TelnyxOkHttpClient.fromEnv();
ChatCreateCompletionParams params = ChatCreateCompletionParams.builder()
.addMessage(ChatCreateCompletionParams.Message.builder()
.content("You are a friendly chatbot.")
.role(ChatCreateCompletionParams.Message.Role.SYSTEM)
.build())
.addMessage(ChatCreateCompletionParams.Message.builder()
.content("Hello, world!")
.role(ChatCreateCompletionParams.Message.Role.USER)
.build())
.build();
ChatCreateCompletionResponse response = client.ai().openai().chat().createCompletion(params);
}
}require "telnyx"
telnyx = Telnyx::Client.new(api_key: "My API Key")
response = telnyx.ai.openai.chat.create_completion(
messages: [{content: "You are a friendly chatbot.", role: :system}, {content: "Hello, world!", role: :user}]
)
puts(response)<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
use Telnyx\Client;
use Telnyx\Core\Exceptions\APIException;
$client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API Key');
try {
$response = $client->ai->openai->chat->createCompletion(
messages: [
['content' => 'You are a friendly chatbot.', 'role' => 'system'],
['content' => 'Hello, world!', 'role' => 'user'],
],
apiKeyRef: 'api_key_ref',
bestOf: 0,
earlyStopping: true,
enableThinking: true,
frequencyPenalty: 0,
guidedChoice: ['string'],
guidedJson: ['foo' => 'bar'],
guidedRegex: 'guided_regex',
lengthPenalty: 0,
logprobs: true,
maxTokens: 0,
minP: 0,
model: 'model',
n: 0,
presencePenalty: 0,
responseFormat: ['type' => 'text'],
seed: 0,
stop: 'string',
stream: true,
temperature: 0,
toolChoice: 'none',
tools: [
[
'function' => [
'name' => 'name',
'description' => 'description',
'parameters' => ['foo' => 'bar'],
],
'type' => 'function',
],
],
topLogprobs: 0,
topP: 0,
useBeamSearch: true,
);
var_dump($response);
} catch (APIException $e) {
echo $e->getMessage();
}telnyx ai:openai:chat create-completion \
--api-key 'My API Key' \
--message '{content: You are a friendly chatbot., role: system}' \
--message "{content: 'Hello, world!', role: user}"curl --request POST \
--url https://api.telnyx.com/v2/ai/openai/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"messages": [
{
"role": "system",
"content": "You are a friendly chatbot."
},
{
"role": "user",
"content": "Hello, world!"
}
]
}
'{
"id": "chatcmpl-8b1f2a3c9d4e5f6a",
"object": "chat.completion",
"created": 1706011802,
"model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Telnyx provides carrier-grade voice, messaging, and AI inference through one API."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 24,
"completion_tokens": 18,
"total_tokens": 42
}
}{
"detail": [
{
"loc": [
"body",
"name"
],
"msg": "Field required",
"type": "missing"
}
]
}