This Cloudflare Worker hosts a Hono application providing an OpenAI-compatible API under /v1. It dynamically loads provider configurations from KV, retrieves secrets from Cloudflare Secrets, and routes through the Cloudflare AI Gateway using Vercel AI SDK instances.
Install dependencies and run locally:
npm install
npm run devDeploy to Cloudflare:
npm run deploy- Add your provider API key as a Cloudflare secret:
npx wrangler secret put MY_PROVIDER_API_KEY- Add a KV entry to
PROVIDER_CONFIGwith your provider config:
npx wrangler kv key put --binding PROVIDER_CONFIG myprovider '{
"provider": "myprovider",
"apiKeySecretName": "MY_PROVIDER_API_KEY",
"gatewayProviderPath": "myprovider-path"
}' --preview--preview helps for testing remove for production
- In
src/index.ts, update theinitializeProvidersswitch:
switch (cfg.provider) {
case "myprovider":
clientFactory = createMyProviderAI({
baseURL: `${url}/v1`,
apiKey,
headers: { "cf-aig-authorization": `Bearer ${env.AI_GATEWAY_TOKEN}` },
});
break;
// existing cases...
}- Regenerate types and verify:
npm run cf-typegen
npm run lint
npm run format
npm run check- Example request:
curl http://localhost:8787/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "myprovider/modelName",
"messages": [{ "role": "user", "content": "Your prompt here" }]
}'For generating/synchronizing types based on your Worker configuration run:
npm run cf-typegen