Overview
/v1/generate — POST — synchronous text-generation endpoint. Accepts JSON body; returns generated text and metadata. Auth: Bearer token via Authorization header. Content-Type: application/json.
Request body schema (application/json)
- prompt (string) — required
- description: input text or instruction for the model
- max length: 50,000 chars
- max_tokens (integer) — optional, default: 256
- temperature (number) — optional, default: 1.0
- valid range: 0.0–2.0 (float)
- semantics: higher = more random
- top_p (number) — optional, default: 1.0
- valid range: 0.0–1.0 (float)
- semantics: nucleus sampling
- stop (array[string] or string) — optional, default: null
- semantics: token(s) where generation stops; max 5 items, each ≤ 100 chars
Headers
- Authorization: Bearer <API_KEY> — required
- Content-Type: application/json — required
- Accept: application/json — recommended
Successful response (200)
Body (application/json):
{
"id": "gen_12345",
"model": "gpt-xyz",
"prompt": "<echoed prompt or id>",
"generated_text": "<string>",
"tokens_used": {
"prompt_tokens": 12,
"completion_tokens": 45,
"total_tokens": 57
},
"finish_reason": "stop" | "length" | "eos",
"created_at": "2025-11-22T12:34:56Z"
}
Error responses
400 Bad Request — invalid input
- Cause examples: missing prompt, invalid types, values outside ranges, too-long prompt, malformed JSON.
- Body:
{
"error": {
"code": "invalid_request",
"message": "prompt is required and must be a string",
"details": {"field":"prompt"}
}
}
429 Too Many Requests — rate limit / quota exceeded
- Headers: Retry-After: seconds
- Body:
{
"error": {
"code": "rate_limit_exceeded",
"message": "Rate limit exceeded. Retry after 30 seconds."
}
}
500 Internal Server Error — server-side
- Body:
{
"error": {
"code": "server_error",
"message": "An unexpected error occurred. Try again later.",
"trace_id": "abc123" // for support/debugging
}
}
Validation and behavior notes
- If both temperature and top_p provided, they are combined per sampling rules; use either or both.
- If stop matched mid-token, model returns finish_reason "stop".
- max_tokens caps generated tokens; may return fewer tokens.
- Rate-limiting and per-account quotas apply; prefer exponential backoff on 429.
Curl example
bash
curl -X POST "https://api.example.com/v1/generate" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Write a concise summary of transformers in NLP.",
"max_tokens": 120,
"temperature": 0.7,
"top_p": 0.9,
"stop": ["\n\n"]
}'
Expected example response (200)
json
{
"id": "gen_01F...",
"model": "gpt-xyz",
"prompt": "Write a concise summary of transformers in NLP.",
"generated_text": "Transformers use self-attention to model long-range dependencies, enabling parallel training and state-of-the-art performance across NLP tasks...",
"tokens_used": {
"prompt_tokens": 8,
"completion_tokens": 42,
"total_tokens": 50
},
"finish_reason": "stop",
"created_at": "2025-11-22T12:34:56Z"
}