← Back to API development
API development / Lesson 04

Run a private API example on Cloudflare Workers

Keep secrets off the teaching site and use a serverless request handler.

Before you begin

Use a Cloudflare account, Node.js, and an API key. Download worker.mjs and wrangler.jsonc to a separate example folder. This is a private shell-client demo with a shared access token, not a public browser chat service or a deployed feature of this site.

1. Understand the request path

The client calls /api/message on your Worker. The Worker checks the access token, validates a short prompt, adds its server-side Claude key, and calls the Messages API. Only the answer travels back. Do not place either the Claude key or the private demo token in browser code. A public product needs individual authentication and enforced quotas.

  1. CLIENTClient

    Sends the prompt with the private access token.

  2. WORKERYour Worker

    Checks the token, validates input, adds the Claude key.

  3. APIMessages API

    Generates the answer; only the answer goes back.

DiagramBoth secrets stay on the Worker; the browser never sees the Claude key.

2. Configure a local test

In the example folder, install Wrangler as a development dependency. Replace the model placeholder in wrangler.jsonc with an accessible ID. Create .dev.vars with ANTHROPIC_API_KEY and a long random DEMO_ACCESS_TOKEN, using your editor; exclude it from version control. Add ANTHROPIC_WORKSPACE_ID if required. Wrangler reads these secrets locally.

shell
npm install --save-dev wrangler@latest
npx wrangler dev

3. Call the local endpoint from a shell

Set DEMO_ACCESS_TOKEN in the calling shell to the same local secret, then run the request. This local Worker still calls the real Claude API and consumes usage. Without authorization expect 401; an empty prompt returns 400; missing configuration returns 503. The example limits input bytes, prompt length, output tokens, and upstream time.

shell
curl --fail-with-body http://localhost:8787/api/message \
  -H "Authorization: Bearer $DEMO_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{"prompt":"Explain Workers in one sentence."}'

4. Prepare deployment deliberately

Use Wrangler secret put interactively for ANTHROPIC_API_KEY and DEMO_ACCESS_TOKEN; do not put them in vars. Configure the model and optional workspace, then use deploy when ready. Keep this example private, monitor usage, and set account spend limits. A public site needs stronger authentication and per-user quotas before exposing a paid endpoint. The handbook does not deploy anything automatically.

shell
npx wrangler secret put ANTHROPIC_API_KEY
npx wrangler secret put DEMO_ACCESS_TOKEN
# If required: npx wrangler secret put ANTHROPIC_WORKSPACE_ID
npx wrangler deploy

Download the example files

Teaching examples; model behavior can vary. Follow the prerequisites and verify outputs. These pages do not send API requests.

Practice & self-check

YOUR TURN / PRACTICE

Start the local Worker. Verify 401 without authorization and 400 for an invalid prompt before trying a real authorized model request.

When you’re done, take a moment to check:

Read the official sources

CLI instructions checked against official documentation; API examples tested with simulated responses. Product-account walkthroughs were not performed.

Was this lesson useful?

Start with a question