Claude Streaming Server (Express)
Claude Streaming Server (Express)
Section titled “Claude Streaming Server (Express)”Latest: 0.68.0 Last verified: 2025-11
import express from "express";import Anthropic from "@anthropic-ai/sdk";
const app = express();const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY! });
app.get("/stream", async (req, res) => { res.setHeader("Content-Type", "text/event-stream"); res.setHeader("Cache-Control", "no-cache"); const q = String(req.query.q || ""); const stream = await client.messages.stream({ model: "claude-3-7-sonnet-2025-07-15", max_tokens: 512, messages: [{ role: "user", content: q }], }); for await (const chunk of stream) { res.write(`data: ${JSON.stringify(chunk)}\n\n`); } res.end();});
app.listen(8080, () => console.log("listening on 8080"));Deployment
Section titled “Deployment”Dockerfile
Section titled “Dockerfile”FROM node:20-slimWORKDIR /appCOPY package*.json ./RUN npm ci --omit=devCOPY . .EXPOSE 8080CMD ["node", "server.js"]Kubernetes (deployment.yaml)
Section titled “Kubernetes (deployment.yaml)”apiVersion: apps/v1kind: Deploymentmetadata: name: claude-ts-streamspec: replicas: 2 selector: matchLabels: app: claude-ts-stream template: metadata: labels: app: claude-ts-stream spec: containers: - name: app image: ghcr.io/yourorg/claude-ts-stream:latest env: [{ name: ANTHROPIC_API_KEY, valueFrom: { secretKeyRef: { name: anthropic-secrets, key: apiKey } } }] ports: [{ containerPort: 8080 }]---apiVersion: v1kind: Servicemetadata: name: claude-ts-streamspec: selector: { app: claude-ts-stream } ports: [{ port: 80, targetPort: 8080 }]GitHub Actions
Section titled “GitHub Actions”name: deployon: { push: { branches: [ main ] } }jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: { node-version: 20 } - run: npm ci --omit=dev - uses: docker/login-action@v3 with: { registry: ghcr.io, username: ${{ github.actor }}, password: ${{ secrets.GITHUB_TOKEN }} } - uses: docker/build-push-action@v5 with: { push: true, tags: ghcr.io/${{ github.repository }}:latest } deploy: needs: build runs-on: ubuntu-latest steps: - uses: azure/k8s-deploy@v5 with: action: deploy manifests: | deployment.yaml images: ghcr.io/${{ github.repository }}:latest namespace: defaultSecurity Best Practices
Section titled “Security Best Practices”- Use secrets from Kubernetes Secret or cloud secret managers
- Authenticate SSE endpoints; enable rate limiting and CORS controls
- Avoid logging request bodies; rotate API keys regularly