This guide shows how to build reliable AI text features using the ApiNest /api/v1/ai-textendpoint. We focus on prompt design, token budgeting, retries, idempotency, and observability so your feature works at scale.
Why ApiNest for AI text
- Predictable costs via points and clear limits
- Consistent JSON responses with usage fields
- Real-time status page and documented error codes
Quick start (JavaScript)
async function generateText(prompt) {
const res = await fetch('https://apinest.com/api/v1/ai-text?q=' + encodeURIComponent(prompt), {
headers: { 'x-api-key': process.env.APINEST_API_KEY }
})
if (!res.ok) throw new Error((await res.json()).message)
return res.json()
}Retries and backoff
Implement exponential backoff when you see 429s or transient 5xx.
async function withRetries(fn, tries = 3) {
for (let i = 0; i < tries; i++) {
try { return await fn() } catch (e) {
if (i === tries - 1) throw e
await new Promise(r => setTimeout(r, Math.pow(2, i) * 1000))
}
}
}Prompt patterns that work
- Instruction + constraints: “Write a 140‑char headline, JSON only with keys title, tone.”
- Few‑shot examples: provide 2–3 formatted samples.
- Guardrails: ask for JSON schema and validate.
Observability checklist
- Log prompt length and completion length
- Capture error code and
retryAfterif present - Sample 1% payloads for quality review
FAQ
How do I keep costs predictable?
Set max_tokens and cache frequent prompts. Use the points table in Docs.
What response format should I expect?
Structured JSON with text, model, and usage fields.
How do I handle timeouts?
Set client timeouts to 30–45s for AI calls and implement retries.
