|
1 | | -# OpenRouter-API-Example |
2 | | -Hello World tutorial for using a LaunchCode-provided OpenRouter API key to access Claude from Python |
| 1 | +# OpenRouter Hello Claude Example |
| 2 | + |
| 3 | +This repo is a beginner-friendly example of how to send a simple Claude request through OpenRouter. |
| 4 | + |
| 5 | +For this exercise, LaunchCode provides a course OpenRouter key. You will store that key in a local file named `claude_cred.txt` and use it with the course-approved model `anthropic/claude-haiku-4.5`. |
| 6 | + |
| 7 | +This tutorial uses only Python's standard library, so you do not need to install any extra Python packages. |
| 8 | + |
| 9 | +## What You Will Learn |
| 10 | + |
| 11 | +- How to keep an API key in a local file instead of pasting it into Python code. |
| 12 | +- How to run a Python script that sends a request to OpenRouter. |
| 13 | +- How to make a successful OpenRouter request to the course-approved model `anthropic/claude-haiku-4.5`. |
| 14 | +- How to recognize common OpenRouter error responses for this course key. |
| 15 | + |
| 16 | +## What You Need |
| 17 | + |
| 18 | +- Python 3 installed on your computer. |
| 19 | +- The LaunchCode-provided course OpenRouter key. |
| 20 | + |
| 21 | +## Get This Project On Your Computer |
| 22 | + |
| 23 | +You do not need a GitHub account to complete this tutorial. |
| 24 | + |
| 25 | +### Option A: Download ZIP |
| 26 | + |
| 27 | +This is the simplest option if you only need to run the example locally. |
| 28 | + |
| 29 | +1. Open `https://github.com/LaunchCodeEducation/OpenRouter-API-Example`. |
| 30 | +2. Click the green **Code** button. |
| 31 | +3. Click **Download ZIP**. |
| 32 | +4. Unzip the downloaded file. |
| 33 | +5. Open the unzipped project folder on your computer. |
| 34 | + |
| 35 | +> The unzipped folder may be named `OpenRouter-API-Example-main`. |
| 36 | +
|
| 37 | +### Option B: Clone With Git |
| 38 | + |
| 39 | +If you already have Git installed, you can clone the repo instead. |
| 40 | + |
| 41 | +```bash |
| 42 | +git clone https://github.com/LaunchCodeEducation/OpenRouter-API-Example.git |
| 43 | +cd OpenRouter-API-Example |
| 44 | +``` |
| 45 | + |
| 46 | +## Add Your OpenRouter Key |
| 47 | + |
| 48 | +1. In the project folder, create a file named `claude_cred.txt`. |
| 49 | +2. Paste the LaunchCode-provided course key into that file. |
| 50 | +3. Make sure the file contains only the key. |
| 51 | + |
| 52 | +Do not add quotes, labels, or extra lines. |
| 53 | + |
| 54 | +## Run The Example |
| 55 | + |
| 56 | +### macOS |
| 57 | + |
| 58 | +Use `python3`, not `python`. On many Macs, `python` may point to a different interpreter or may not be available in the way you expect. |
| 59 | + |
| 60 | +```bash |
| 61 | +python3 hello_claude.py |
| 62 | +``` |
| 63 | + |
| 64 | +### Windows |
| 65 | + |
| 66 | +```bat |
| 67 | +py hello_claude.py |
| 68 | +``` |
| 69 | + |
| 70 | +If everything is set up correctly, the script prints a short reply from Claude. |
| 71 | + |
| 72 | +## Keep Your Key Safe |
| 73 | + |
| 74 | +- Do not paste your course key into `hello_claude.py` or any other Python file. |
| 75 | +- Do not share your API key with other people. |
| 76 | +- Keep `claude_cred.txt` on your own computer. |
| 77 | +- This repo already lists `claude_cred.txt` in `.gitignore`, which helps prevent accidental commits if you later use Git. |
| 78 | + |
| 79 | +You do not need to commit or upload anything to complete this tutorial. |
| 80 | + |
| 81 | +If you later decide to store your work in Git or GitHub, make sure files that contain secrets stay ignored and never get uploaded. See GitHub Docs: [Ignoring files](https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files). |
| 82 | + |
| 83 | +## Files In This Repo |
| 84 | + |
| 85 | +- `hello_claude.py`: the minimal Python example for the course-approved OpenRouter call. |
| 86 | +- `example-open-router-responses/successful_call_example.json`: an example JSON response from a successful request. |
| 87 | +- `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. |
| 88 | +- `example-open-router-responses/claude-haiku-4.5_model-details.json`: reference details for the course-approved model from OpenRouter. |
| 89 | + |
| 90 | +You send the alias `anthropic/claude-haiku-4.5`, but the JSON response may show a more specific provider model version in the `model` field. |
| 91 | + |
| 92 | +## Troubleshooting |
| 93 | + |
| 94 | +- If Python says it cannot find `claude_cred.txt`, make sure that file is in the same folder as `hello_claude.py`. |
| 95 | +- `401 Unauthorized` usually means `claude_cred.txt` does not contain the correct key, or it contains extra whitespace or extra text. |
| 96 | +- `404` usually means you tried to use a model outside the access allowed for your course key. For this course, use only `anthropic/claude-haiku-4.5`. |
| 97 | +- If your computer says `python3` or `py` is not recognized, Python may not be installed or may not be available in your terminal yet. |
| 98 | +- If the script says it could not reach OpenRouter, check your internet connection and try again. |
| 99 | + |
| 100 | +## OpenRouter Language |
| 101 | + |
| 102 | +This example uses Claude through OpenRouter, not through Anthropic's direct API. |
| 103 | + |
| 104 | +That means: |
| 105 | + |
| 106 | +- the credential file is still named `claude_cred.txt` |
| 107 | +- the key is still read from a local text file at runtime |
| 108 | +- the network request goes to OpenRouter |
| 109 | +- the course-approved model is `anthropic/claude-haiku-4.5` |
0 commit comments