Openrouter Hello World
Overview
This skill provides a minimal working example to verify your OpenRouter integration is functioning and introduces the basic request/response pattern.
Prerequisites
- OpenRouter API key configured
- Python 3.8+ or Node.js 18+
- OpenAI SDK installed
Instructions
- Set your API key: Export
OPENROUTER_API_KEYas an environment variable or add it to your.envfile - Send a test completion: Make a POST request to
https://openrouter.ai/api/v1/chat/completionswith a simple prompt usingmodel: "openai/gpt-3.5-turbo" - Verify the response: Confirm the response contains
choices[0].message.contentandusagetoken counts - Try a different model: Swap the model to
anthropic/claude-3-haikuorgoogle/gemma-2-9b-it:freeto confirm multi-model routing works
Output
- A JSON response with
id,model,choices, andusagefields - Token usage breakdown (prompt_tokens, completion_tokens, total_tokens)
- Confirmation that your API key and network configuration are correct
Error Handling
| Error | Cause | Fix |
|---|---|---|
| 401 Unauthorized | Invalid or missing API key | Verify key starts with sk-or- and is exported correctly |
| 404 Not Found | Wrong base URL | Use https://openrouter.ai/api/v1 (not v2) |
| 400 Bad Request | Malformed JSON body | Check messages array has role and content fields |
See ${CLAUDE_SKILL_DIR}/references/errors.md for full error reference.
Examples
See ${CLAUDE_SKILL_DIR}/references/examples.md for runnable code samples.