PinMe Worker OpenRouter API Integration
Guides how to call PinMe platform's OpenRouter proxy APIs in a PinMe Worker (TypeScript). Workers use the PinMe project API key; they never hold the real OpenRouter API key.
Environment Variables
The following environment variables are automatically injected when the Worker is created — no manual configuration needed:
API_KEYauthenticates the Worker to PinMe.PROJECT_NAMEis required forchat/completionsand must belong to the same project asAPI_KEY. WhenBASE_URLis not set, usehttps://pinme.cloud.
Models API
Endpoint: GET {BASE_URL}/api/v1/models
Authentication: X-API-Key header (using env.API_KEY)
Request Body: none
Use this when the Worker needs to list available OpenRouter models. The response body, status, and headers are passed through from OpenRouter /models.
Chat Completions API
Endpoint: POST {BASE_URL}/api/v1/chat/completions?project_name={project_name}
Authentication: X-API-Key header (using env.API_KEY)
Request Body: OpenRouter chat/completions format, passed through as-is after a 1MB size check
Streaming: Supports SSE (stream: true)
Web Search: Supports OpenRouter openrouter:web_search server tool via the tools array
Request Format
Use
env.PROJECT_NAMEfromcreate_worker; always URL-encode it in the query string. For available models, callGET /api/v1/modelsor refer to OpenRouter model IDs.
OpenRouter Web Search
PinMe does not provide a raw search endpoint. To search the web, pass OpenRouter's openrouter:web_search server tool to chat/completions; the model decides whether and when to search.
Always set max_results and max_total_results to keep search volume and cost bounded.
Response Format
Successful requests return OpenRouter's raw response body.
Non-streaming Success (200):
Streaming Success (200): SSE format
Errors:
If OpenRouter receives the request and returns a 4xx/5xx, PinMe passes through OpenRouter's status, headers, and response body instead of wrapping it.
Worker Example Code — Non-streaming
Worker Example Code — Streaming (SSE Passthrough)
Frontend SSE Stream Consumer Example
Error Handling Pattern
For /api/v1/models and /api/v1/chat/completions, successful responses are raw OpenRouter responses. Proxy failures before the OpenRouter request use PinMe's wrapped error format:
Recommended Error Extractor
Optional JSON Helper
Use this helper for non-streaming POST calls. It returns the raw OpenRouter JSON on success.


