Leverage the flexibility and power of our Chat Completions endpoint to integrate conversational AI into your applications. Whether you're building customer support bots, generating creative content, or crafting interactive experiences, our chat completion API is designed to help you get the most from AI models while providing advanced features like RAG integration, model routing, and response verification.
The Chat Completions API allows developers to interact with AI models in a conversational manner by sending a sequence of messages to the model and receiving a response. The structure of these messages is fully compliant with OpenAI's API structure, allowing seamless integration.
Our API is fully compatible with the OpenAI Chat Completions API, making migration seamless. Just change the URL and your API key, and your OpenAI-based application can start using APIpie immediately. Once integrated, you can take advantage of our additional features:
A typical API call sends an array of messages to the chosen model, where each message consists of a role (either system, user, or assistant) and content. The model then processes the conversation and returns a response based on the provided context.
Our API integrates with multiple leading AI providers, each offering unique capabilities:
See our Models Overview for a complete list of supported models and providers.
Below is an example of how to use the Chat Completions API to generate a response:
curl -L -X POST 'https://apipie.ai/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
--data-raw '{
"provider": "openrouter",
"model": "gpt-4o",
"max_tokens": 100,
"messages": [
{
"role": "user",
"content": "Why is the sky blue?"
}
]
}'
The expected response structure looks like the following:
{
"id": "chatcmpl-5fde5f7fffe8d6dc1f18aab4a138d4b7",
"object": "chat.completion",
"created": 1729535643,
"provider": "openrouter",
"model": "openai/gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The sky appears blue primarily due to a phenomenon called Rayleigh scattering. Here's how it works:\n\n1. **Sunlight Composition**: Sunlight, or white light, is composed of many colors, each with different wavelengths. These colors can be seen in a rainbow or through a prism.\n\n2. **Atmospheric Interaction**: As sunlight reaches the Earth's atmosphere, it collides with molecules of gases and small particles.\n\n3. **Scattering and Wavelengths**: Different colors of light are"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 13,
"completion_tokens": 100,
"total_tokens": 113,
"prompt_characters": 20,
"response_characters": 474,
"cost": 0.001878,
"latency_ms": 2727
},
"system_fingerprint": "fp_f4d98523ab4ae852"
}
This structure provides the generated message, completion details, and detailed usage metrics for each request.
system, user, assistant.{
"role": "system",
"content": "You are a helpful assistant that speaks only in Swedish."
}
{
"role": "user",
"content": "Why is the sky blue?"
}
{
"role": "assistant",
"content": "The sky is blue because of a phenomenon called Rayleigh scattering..."
}
price: Chooses the cheapest provider.perf: Selects based on lowest latency for prompt size.perf_avg: Chooses based on average latency.Our Integrated Model Memory (IMM) system provides advanced conversation context management that works across all supported models:
Example API call with memory management:
curl -L -X POST 'https://apipie.ai/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
--data-raw '{
"memory": 1,
"mem_session": "user123",
"mem_expire": 60,
"messages": [
{
"role": "user",
"content": "Remember this: my favorite color is blue"
}
]
}'
This enables persistent conversation context across different models within the same session, allowing you to maintain context even when switching between different AI providers.
You can fine-tune the model's output by controlling randomness, creativity, and repetition through the following parameters:
Here’s a sample API call with these options:
curl -L -X POST 'https://apipie.ai/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
--data-raw '{
"provider": "openrouter",
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Tell me a story about a talking cat."
}
],
"temperature": 0.7,
"top_p": 0.9,
"top_k": 50,
"frequency_penalty": 0.2,
"presence_penalty": 0.5
}'
Note: This is not available on all models, if the model you support does not support it, the parameter will be dropped.
curl -L -X POST 'https://apipie.ai/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
--data-raw '{
"provider": "openrouter",
"model": "llama-3.2-1b-instruct",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"beam_size": 5
}'
curl -L -X POST 'https://apipie.ai/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
--data-raw '{
"provider": "openrouter",
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "How does streaming work in AI?"
}
],
"stream": true
}'
Our unique integrity feature helps ensure response accuracy and reduce hallucinations:
Learn more about response verification in our Integrity Guide.
curl -L -X POST 'https://apipie.ai/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
--data-raw '{
"provider": "openrouter",
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Why is the sky blue?"
}
],
"integrity": 13,
"integrity_model": "gpt-3.5-turbo"
}'
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"provider":"openrouter","model":"openai/gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}],"system_fingerprint":"null"}
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"provider":"openrouter","model":"openai/gpt-4o","choices":[{"index":1,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"system_fingerprint":"null"}
...
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"provider":"openrouter","model":"openai/gpt-4o","choices":[{"index":100,"delta":{"content":" sky"},"logprobs":null,"finish_reason":null}],"system_fingerprint":"null"}
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"provider":"openrouter","model":"openai/gpt-4o","choices":[{"index":101,"delta":{"content":""},"logprobs":null,"finish_reason":null}],"system_fingerprint":"null"}
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"provider":"openrouter","model":"openai/gpt-4o","choices":[{"index":102,"delta":{"content":""},"logprobs":null,"finish_reason":null}],"system_fingerprint":"null"}
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"provider":"openrouter","model":"openai/gpt-4o","choices":[{"index":0,"delta":{"content":""},"logprobs":null,"finish_reason":"stop"}],"system_fingerprint":"null"}
data: {"id":"chatcmpl-fc2e9121675cd68b07b6d1eb5e2b11e8","object":"chat.completion.chunk","created":1729535567,"model":"openai/gpt-4o","system_fingerprint":"null","choices":[],"usage":{"prompt_tokens":13,"completion_tokens":100,"total_tokens":113,"prompt_characters":20,"response_characters":527,"cost":0.001878,"latency_ms":2831}}
data: [DONE]
We provide comprehensive usage data with every request to help you track costs and performance. Metrics include:
prompt_tokens and completion_tokens.{
"id": "chatcmpl-5fde5f7fffe8d6dc1f18aab4a138d4b7",
"object": "chat.completion",
"created": 1729535643,
"provider": "openrouter",
"model": "openai/gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The sky appears blue primarily due to a phenomenon called Rayleigh scattering. Here's how it works..."
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 13,
"completion_tokens": 100,
"total_tokens": 113,
"prompt_characters": 20,
"response_characters": 474,
"cost": 0.001878,
"latency_ms": 2727
},
"system_fingerprint": "fp_f4d98523ab4ae852"
}
Note: We are a leader in query usage reporting, offering extensive data tracking for every request. Additionally, historical billing data is available via API for audit purposes.
Each provider has specific rate limits and quotas. For optimal performance:
Different models have varying capabilities and limitations:
For detailed model comparisons and performance metrics, see our Models Guide.
Common error scenarios and recommended handling:
{
"error": {
"message": "Error description",
"type": "invalid_request_error",
"param": "messages",
"code": "context_length_exceeded"
}
}
To ensure secure API usage:
Models Route Guide: Filter and Select AI Models
Discover how to fetch and filter AI models through our API, enabling precise selections for your applications. Start optimizing now!
AI Image Generation: DALL-E, Stable Diffusion, & More
Create stunning AI images using our powerful API with DALL-E 3, Stable Diffusion XL, and more. Experience professional quality today!