Use an OpenAI-compatible API in a Next.js Route Handler
Use the OpenAI JavaScript SDK from the server to call a supported chat model in a Next.js App Router project.
Updated
Set server environment variables
Run npm install openai. Set CLANKER_API_KEY and CLANKER_MODEL in .env.local. Use an enabled chat model from your key’s model list. Keep the key out of source control and avoid the NEXT_PUBLIC_ prefix.
Add a local test route
Save as app/api/model-smoke/route.ts (or src/app/api/model-smoke/route.ts). Use it locally. Before deploying, remove the smoke route or require authentication and rate limits so visitors cannot spend your credit.
import OpenAI from "openai";
export async function POST() {
const apiKey = process.env.CLANKER_API_KEY;
const model = process.env.CLANKER_MODEL;
if (!apiKey || !model) {
return Response.json({ error: "Missing API configuration." }, { status: 500 });
}
const client = new OpenAI({ apiKey, baseURL: "https://api.cheapclankers.com/v1", maxRetries: 0 });
try {
const result = await client.chat.completions.create({
model,
messages: [{ role: "user", content: "Say hello." }],
max_tokens: 128,
});
return Response.json({ text: result.choices[0]?.message.content ?? "" });
} catch (error) {
if (error instanceof OpenAI.APIError) {
console.error("Model request failed", error.status, error.request_id);
}
return Response.json({ error: "Model request failed." }, { status: 502 });
}
}Test the route
Start Next.js on port 3000. Expect JSON with a text field. For a 502, look up the upstream status and request ID from the server log. Check usage before retrying a timed-out generation.
curl -i -X POST http://localhost:3000/api/model-smokeOfficial tool documentation
These examples describe the endpoint configuration. Review current SDK or tool requirements before installing; live inference and tool support depend on the selected model.
Next steps
API documentation · Live model catalogue · Compare credit packs
More tutorials: Add CheapClankers as a LibreChat custom endpoint · Use the OpenAI Node SDK with a custom API base URL · Use the OpenAI Python SDK with a custom API base URL · Configure Claude Code to use the CheapClankers API · Codex CLI setup
New to the service? Create a developer account and review the current model access and rates before adding credit.