This repo shows how to send a simple Claude request through OpenRouter.
LaunchCode has provided you with an OpenRouter API key. Store it in claude_cred.txt and use it with anthropic/claude-haiku-4.5.
The example uses Python and the requests library.
- Store an API key in a local file instead of hard-coding it in Python.
- Run a Python script that sends a request to OpenRouter.
- Send a successful request to
anthropic/claude-haiku-4.5. - Recognize common OpenRouter errors for this course key.
To check whether Python 3 is installed, open a terminal and run:
macOS or Linux:
python3 --versionWindows:
py --versionIf you see a Python version such as Python 3.12.4, Python 3 is installed.
If your computer says the command is not recognized, download Python 3 from Python Downloads.
If you do not have Visual Studio Code yet, download it from Visual Studio Code Download.
Have your LaunchCode-provided OpenRouter API key ready before you begin.
You do not need a GitHub account to complete this tutorial.
This is the simplest option if you only need to run the example locally.
- Open the OpenRouter-API-Example repository.
- Click the green Code button.
- Click Download ZIP.
- Unzip the downloaded file.
- Move it to your
Documentsfolder. - Open the unzipped project folder on your computer using Visual Studio Code.
The unzipped folder may be named
OpenRouter-API-Example-main.
If you already have Git installed, you can clone the repo instead.
cd path-on-your-computer-you-want-project-to-be-cloned-to
git clone https://github.com/LaunchCodeEducation/OpenRouter-API-Example.git
cd OpenRouter-API-Example- In the project folder, create a file named
claude_cred.txt. - Paste your OpenRouter API key into this file, then save it.
- Make sure the file contains only the key, with no quotes, labels, or extra lines.
After you open this project folder in Visual Studio Code:
- At the top of the Visual Studio Code window, click Terminal in the menu bar.
- In the dropdown menu, click New Terminal.
- A terminal panel will open at the bottom of the VS Code window.
- Make sure the terminal is in this project folder, where
hello_claude.pyis located.
To check your current folder, run the command for your operating system:
macOS or Linux:
pwd
lsWindows:
pwd
dirYou should see the project folder path, and hello_claude.py should appear in the file list.
If not, use cd to move into the project folder.
If you downloaded the ZIP file:
cd ~/Documents/OpenRouter-API-Example-mainIf you used git clone:
cd ~/Documents/OpenRouter-API-ExampleYou will run the Python command in that VS Code terminal window.
This project uses a local virtual environment named .venv. It keeps this project's Python package installation separate from other Python projects on your computer.
Use the terminal window inside Visual Studio Code.
On macOS, use python3 for the commands in this tutorial. Depending on how Python is installed on your Mac, python may point to a different interpreter or may not be available.
python3 -m venv .venv
source .venv/bin/activateUse the default VS Code terminal window.
py -m venv .venv
.\.venv\Scripts\Activate.ps1When the environment is active, your terminal prompt usually starts with (.venv).
With the virtual environment activated, run:
python3 -m pip install -r requirements.txtpython -m pip install -r requirements.txtWith the virtual environment activated, run:
python3 hello_claude.pypython hello_claude.pyIf everything is set up correctly, the script prints a short reply from Claude.
When you are done working in this project, run:
deactivateThis returns your terminal to the system Python. If your prompt still starts with (.venv), the environment is still active.
- Do not paste your course key into
hello_claude.pyor any other Python file. - Do not share your API key with other people.
- Keep
claude_cred.txton your own computer. - This repo already lists
claude_cred.txtin.gitignore, which helps prevent accidental commits if you later use Git.
You do not need to commit or upload anything to complete this tutorial.
If you later use Git or GitHub, keep secret files ignored and never upload them. See GitHub Docs: Ignoring files.
hello_claude.py: the minimal Python example for the course-approved OpenRouter call.requirements.txt: the Python dependency list for this project.example-open-router-responses/successful_call_example.json: an example JSON response from a successful request.example-open-router-responses/example_404_guardrail_error.json: an example JSON response returned when the request tries a model the course key cannot access.example-open-router-responses/claude-haiku-4.5_model-details.json: reference details for the course-approved model from OpenRouter.
You send the alias anthropic/claude-haiku-4.5, but the response may show a more specific provider model in the model field.
- Missing
claude_cred.txt: If Python says it cannot findclaude_cred.txt, make sure that file is in the same folder ashello_claude.py. ModuleNotFoundError: No module named 'requests': Your virtual environment is not activated yet, or you have not run thepip install -r requirements.txtcommand for your operating system.401 Unauthorized:claude_cred.txtcontains the wrong key, extra whitespace, or extra text. Also make sure you saved the file after pasting in your key.404 from OpenRouter: You tried to use a model outside the access allowed for your course key. For this course, use onlyanthropic/claude-haiku-4.5.python3,py, orpythonis not recognized: Python may not be installed yet. If you just installed Python, close the terminal, open a new one, and try the command again.- Could not reach OpenRouter: Check your internet connection and try again.
- SSL certificate errors: These are less common with the
requestslibrary, but they can still happen on some school, work, or proxy-managed networks. If this happens, use your Claude chat account or Claude desktop/web app to help troubleshoot the Python environment, certificate store, or proxy settings. Do not use your OpenRouter API key for troubleshooting. - PowerShell blocks
.\.venv\Scripts\Activate.ps1: Switch the VS Code terminal profile to Command Prompt and use.venv\Scripts\activate.batinstead.