A Cloudflare Workers-based AI API server that provides streaming chat completions.
- 🤖 AI-powered chat completions
- 📡 Server-sent events (SSE) streaming responses
- 🛡️ Robust error handling with structured JSON responses
- ⚡ Built on Cloudflare Workers for global edge deployment
- 🔒 Input validation and content-type enforcement
- Node.js 18+
- Cloudflare account with Workers AI enabled
- Wrangler CLI
- Clone the repository:
git clone <repository-url>
cd pinac-nexus
- Install dependencies:
npm install
- Configure Wrangler:
npx wrangler login
- Deploy to Cloudflare Workers:
npm run deploy
Run locally with Wrangler:
npm run dev
Note
It will be accessed through API-Getway and need user ID-Token for authentication
// Using fetch API
const response = await fetch(
"https://api-getway-url/api/ai",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_USER_ID_TOKEN",
},
body: JSON.stringify({
messages: [
{
role: "user",
content: "Write a short poem about coding",
},
],
}),
}
);
// Handle streaming response
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = new TextDecoder().decode(value);
console.log(chunk);
}
The server uses the following AI model configuration:
- Model:
@cf/google/gemma-3-12b-it
- Max tokens: 2096
- Streaming: Always enabled
Error Code | Description | Status Code |
---|---|---|
METHOD_NOT_ALLOWED |
Only POST requests are allowed | 405 |
INVALID_CONTENT_TYPE |
Request missing required JSON content-type | 415 |
BAD_REQUEST |
Messages field missing or invalid | 400 |
AI_SERVER_ERROR |
Server processing error | 500 |
@RajeshTechForge