Create text completion
curl --request POST \
--url https://cloud-api.near.ai/v1/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"prompt": "<string>",
"best_of": 123,
"echo": true,
"frequency_penalty": 123,
"logprobs": 123,
"max_tokens": 123,
"n": 123,
"presence_penalty": 123,
"stop": "<string>",
"stream": true,
"temperature": 123,
"top_p": 123
}
'import requests
url = "https://cloud-api.near.ai/v1/completions"
payload = {
"model": "<string>",
"prompt": "<string>",
"best_of": 123,
"echo": True,
"frequency_penalty": 123,
"logprobs": 123,
"max_tokens": 123,
"n": 123,
"presence_penalty": 123,
"stop": "<string>",
"stream": True,
"temperature": 123,
"top_p": 123
}
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: '<string>',
prompt: '<string>',
best_of: 123,
echo: true,
frequency_penalty: 123,
logprobs: 123,
max_tokens: 123,
n: 123,
presence_penalty: 123,
stop: '<string>',
stream: true,
temperature: 123,
top_p: 123
})
};
fetch('https://cloud-api.near.ai/v1/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://cloud-api.near.ai/v1/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>',
'prompt' => '<string>',
'best_of' => 123,
'echo' => true,
'frequency_penalty' => 123,
'logprobs' => 123,
'max_tokens' => 123,
'n' => 123,
'presence_penalty' => 123,
'stop' => '<string>',
'stream' => true,
'temperature' => 123,
'top_p' => 123
]),
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://cloud-api.near.ai/v1/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"prompt\": \"<string>\",\n \"best_of\": 123,\n \"echo\": true,\n \"frequency_penalty\": 123,\n \"logprobs\": 123,\n \"max_tokens\": 123,\n \"n\": 123,\n \"presence_penalty\": 123,\n \"stop\": \"<string>\",\n \"stream\": true,\n \"temperature\": 123,\n \"top_p\": 123\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://cloud-api.near.ai/v1/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"prompt\": \"<string>\",\n \"best_of\": 123,\n \"echo\": true,\n \"frequency_penalty\": 123,\n \"logprobs\": 123,\n \"max_tokens\": 123,\n \"n\": 123,\n \"presence_penalty\": 123,\n \"stop\": \"<string>\",\n \"stream\": true,\n \"temperature\": 123,\n \"top_p\": 123\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://cloud-api.near.ai/v1/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\": \"<string>\",\n \"prompt\": \"<string>\",\n \"best_of\": 123,\n \"echo\": true,\n \"frequency_penalty\": 123,\n \"logprobs\": 123,\n \"max_tokens\": 123,\n \"n\": 123,\n \"presence_penalty\": 123,\n \"stop\": \"<string>\",\n \"stream\": true,\n \"temperature\": 123,\n \"top_p\": 123\n}"
response = http.request(request)
puts response.read_body{
"choices": [
{
"index": 123,
"text": "<string>",
"finish_reason": "<string>",
"logprobs": "<unknown>"
}
],
"created": 123,
"id": "<string>",
"model": "<string>",
"object": "<string>",
"usage": {
"completion_tokens": 123,
"prompt_tokens": 123,
"total_tokens": 123,
"completion_tokens_details": {
"reasoning_tokens": 123
},
"prompt_tokens_details": {
"cached_tokens": 123
}
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}Chat
Create text completion
Generate AI model responses for text prompts. OpenAI-compatible endpoint.
POST
/
v1
/
completions
Create text completion
curl --request POST \
--url https://cloud-api.near.ai/v1/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"prompt": "<string>",
"best_of": 123,
"echo": true,
"frequency_penalty": 123,
"logprobs": 123,
"max_tokens": 123,
"n": 123,
"presence_penalty": 123,
"stop": "<string>",
"stream": true,
"temperature": 123,
"top_p": 123
}
'import requests
url = "https://cloud-api.near.ai/v1/completions"
payload = {
"model": "<string>",
"prompt": "<string>",
"best_of": 123,
"echo": True,
"frequency_penalty": 123,
"logprobs": 123,
"max_tokens": 123,
"n": 123,
"presence_penalty": 123,
"stop": "<string>",
"stream": True,
"temperature": 123,
"top_p": 123
}
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: '<string>',
prompt: '<string>',
best_of: 123,
echo: true,
frequency_penalty: 123,
logprobs: 123,
max_tokens: 123,
n: 123,
presence_penalty: 123,
stop: '<string>',
stream: true,
temperature: 123,
top_p: 123
})
};
fetch('https://cloud-api.near.ai/v1/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://cloud-api.near.ai/v1/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>',
'prompt' => '<string>',
'best_of' => 123,
'echo' => true,
'frequency_penalty' => 123,
'logprobs' => 123,
'max_tokens' => 123,
'n' => 123,
'presence_penalty' => 123,
'stop' => '<string>',
'stream' => true,
'temperature' => 123,
'top_p' => 123
]),
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://cloud-api.near.ai/v1/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"prompt\": \"<string>\",\n \"best_of\": 123,\n \"echo\": true,\n \"frequency_penalty\": 123,\n \"logprobs\": 123,\n \"max_tokens\": 123,\n \"n\": 123,\n \"presence_penalty\": 123,\n \"stop\": \"<string>\",\n \"stream\": true,\n \"temperature\": 123,\n \"top_p\": 123\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://cloud-api.near.ai/v1/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"prompt\": \"<string>\",\n \"best_of\": 123,\n \"echo\": true,\n \"frequency_penalty\": 123,\n \"logprobs\": 123,\n \"max_tokens\": 123,\n \"n\": 123,\n \"presence_penalty\": 123,\n \"stop\": \"<string>\",\n \"stream\": true,\n \"temperature\": 123,\n \"top_p\": 123\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://cloud-api.near.ai/v1/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\": \"<string>\",\n \"prompt\": \"<string>\",\n \"best_of\": 123,\n \"echo\": true,\n \"frequency_penalty\": 123,\n \"logprobs\": 123,\n \"max_tokens\": 123,\n \"n\": 123,\n \"presence_penalty\": 123,\n \"stop\": \"<string>\",\n \"stream\": true,\n \"temperature\": 123,\n \"top_p\": 123\n}"
response = http.request(request)
puts response.read_body{
"choices": [
{
"index": 123,
"text": "<string>",
"finish_reason": "<string>",
"logprobs": "<unknown>"
}
],
"created": 123,
"id": "<string>",
"model": "<string>",
"object": "<string>",
"usage": {
"completion_tokens": 123,
"prompt_tokens": 123,
"total_tokens": 123,
"completion_tokens_details": {
"reasoning_tokens": 123
},
"prompt_tokens_details": {
"cached_tokens": 123
}
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>",
"param": "<string>"
}
}Authorizations
API key for programmatic access (Authorization: Bearer sk-<api_key>)
Body
application/json
OpenAI /v1/completions prompt: a single string, a batch of strings, or
token-ID array(s). This endpoint serves only the single-string form; the
other shapes still deserialize (so the handler can return a clean 400 rather
than a framework deserialization error) and are rejected there.
OpenAI stop: either a single string or an array of strings.
Response
Completion generated successfully
Show child attributes
Show child attributes
Usage for chat/completions endpoints. Serializes as prompt_tokens, completion_tokens, prompt_tokens_details, completion_tokens_details, total_tokens.
Show child attributes
Show child attributes