Skip to content

Commit edb4170

Browse files
Use requests and document venv setup
- Switch the example from urllib to requests to reduce SSL certificate friction for learners using local Python installs. - Add a tracked requirements.txt and update the README to teach creating, activating, and deactivating a local virtual environment, installing dependencies, and handling common setup errors.
1 parent 91ad63e commit edb4170

3 files changed

Lines changed: 162 additions & 35 deletions

File tree

README.md

Lines changed: 141 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This repo is a beginner-friendly example of how to send a simple Claude request
44

55
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`.
66

7-
This tutorial uses only Python's standard library, so you do not need to install any extra Python packages.
7+
This tutorial uses the `requests` library to make the OpenRouter call.
88

99
## What You Will Learn
1010

@@ -16,7 +16,30 @@ This tutorial uses only Python's standard library, so you do not need to install
1616
## What You Need
1717

1818
- Python 3 installed on your computer.
19-
- The LaunchCode-provided course OpenRouter key.
19+
20+
To check whether Python 3 is installed, open a terminal and run:
21+
22+
macOS or Linux:
23+
24+
```bash
25+
python3 --version
26+
```
27+
28+
Windows:
29+
30+
```powershell
31+
py --version
32+
```
33+
34+
If you see a Python version such as `Python 3.12.4`, Python 3 is installed.
35+
36+
If your computer says the command is not recognized, download Python 3 from `https://www.python.org/downloads/`.
37+
38+
- Visual Studio Code installed on your computer.
39+
40+
If you do not have Visual Studio Code yet, download it from `https://code.visualstudio.com/Download`.
41+
42+
- Your LaunchCode-provided OpenRouter API key.
2043

2144
## Get This Project On Your Computer
2245

@@ -57,35 +80,136 @@ Do not add quotes, labels, or extra lines.
5780

5881
After you open this project folder in Visual Studio Code:
5982

60-
1. Click **Terminal** in the top menu.
61-
2. Click **New Terminal**.
83+
1. At the top of the Visual Studio Code window, click **Terminal** in the menu bar.
84+
2. In the dropdown menu, click **New Terminal**.
6285
3. A terminal panel will open at the bottom of the VS Code window.
6386
4. Make sure the terminal is in this project folder, where `hello_claude.py` is located.
6487

88+
To check your current folder:
89+
90+
macOS or Linux:
91+
92+
```bash
93+
pwd
94+
ls
95+
```
96+
97+
Windows:
98+
99+
```powershell
100+
pwd
101+
dir
102+
```
103+
104+
You should see this project folder path, and `hello_claude.py` should appear in the file list.
105+
106+
If you are not in the correct folder yet, use `cd` to move into it.
107+
108+
If you downloaded the ZIP file:
109+
110+
```bash
111+
cd ~/Documents/OpenRouter-API-Example-main
112+
```
113+
114+
If you used `git clone`:
115+
116+
```bash
117+
cd ~/Documents/OpenRouter-API-Example
118+
```
119+
65120
You will run the Python command in that VS Code terminal window.
66121

67-
## Run The Example
122+
## Create And Activate A Virtual Environment
123+
124+
This project uses a local Python virtual environment named `.venv`.
125+
126+
That keeps this project's Python package installation separate from other Python projects on your computer.
68127

69128
Use the terminal window inside Visual Studio Code.
70129

71130
### macOS
72131

73-
Use `python3`, not `python`. On many Macs, `python` may point to a different interpreter or may not be available in the way you expect.
132+
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.
74133

75134
```bash
76-
python3 hello_claude.py
135+
python3 -m venv .venv
136+
source .venv/bin/activate
137+
```
138+
139+
### Linux
140+
141+
```bash
142+
python3 -m venv .venv
143+
source .venv/bin/activate
77144
```
78145

79146
### Windows
80147

81-
In the VS Code terminal, run:
148+
Use the default VS Code terminal window.
82149

83-
```bat
84-
py hello_claude.py
150+
```powershell
151+
py -m venv .venv
152+
.\.venv\Scripts\Activate.ps1
153+
```
154+
155+
When the environment is active, your terminal prompt usually starts with `(.venv)`.
156+
157+
## Install The Project Dependency
158+
159+
With the virtual environment activated, run the command for your operating system.
160+
161+
### macOS
162+
163+
```bash
164+
python3 -m pip install -r requirements.txt
165+
```
166+
167+
### Linux
168+
169+
```bash
170+
python -m pip install -r requirements.txt
171+
```
172+
173+
### Windows
174+
175+
```powershell
176+
python -m pip install -r requirements.txt
177+
```
178+
179+
## Run The Example
180+
181+
Use the terminal window inside Visual Studio Code.
182+
183+
With the virtual environment activated, run the command for your operating system.
184+
185+
### macOS
186+
187+
```bash
188+
python3 hello_claude.py
189+
```
190+
191+
### Linux
192+
193+
```bash
194+
python hello_claude.py
195+
```
196+
197+
### Windows
198+
199+
```powershell
200+
python hello_claude.py
85201
```
86202

87203
If everything is set up correctly, the script prints a short reply from Claude.
88204

205+
## Deactivate The Virtual Environment
206+
207+
When you are done working in this project, run:
208+
209+
```bash
210+
deactivate
211+
```
212+
89213
## Keep Your Key Safe
90214

91215
- Do not paste your course key into `hello_claude.py` or any other Python file.
@@ -100,19 +224,23 @@ If you later decide to store your work in Git or GitHub, make sure files that co
100224
## Files In This Repo
101225

102226
- `hello_claude.py`: the minimal Python example for the course-approved OpenRouter call.
227+
- `requirements.txt`: the Python dependency list for this project.
103228
- `example-open-router-responses/successful_call_example.json`: an example JSON response from a successful request.
104229
- `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.
105230
- `example-open-router-responses/claude-haiku-4.5_model-details.json`: reference details for the course-approved model from OpenRouter.
106231

107232
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.
108233

109-
## Troubleshooting
234+
## Common Python Errors
110235

111236
- If Python says it cannot find `claude_cred.txt`, make sure that file is in the same folder as `hello_claude.py`.
237+
- `ModuleNotFoundError: No module named 'requests'` usually means your virtual environment is not activated yet, or you have not run `python -m pip install -r requirements.txt`.
112238
- `401 Unauthorized` usually means `claude_cred.txt` does not contain the correct key, or it contains extra whitespace or extra text.
113-
- `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`.
114-
- If your computer says `python3` or `py` is not recognized, Python may not be installed or may not be available in your terminal yet.
239+
- `404 from OpenRouter` 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`.
240+
- If your computer says `python3`, `py`, or `python` is 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.
115241
- If the script says it could not reach OpenRouter, check your internet connection and try again.
242+
- SSL certificate errors are less common with `requests`, but they can still happen on some school, work, or proxy-managed networks.
243+
- On Windows, if PowerShell blocks `.\.venv\Scripts\Activate.ps1`, switch the VS Code terminal profile to Command Prompt and use `.venv\Scripts\activate.bat` instead.
116244

117245
## OpenRouter Language
118246

hello_claude.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
2-
import urllib.error
3-
import urllib.request
1+
import requests
42

53

64
MODEL = "anthropic/claude-haiku-4.5"
@@ -30,36 +28,36 @@
3028
],
3129
}
3230

33-
request = urllib.request.Request(
34-
"https://openrouter.ai/api/v1/chat/completions",
35-
data=json.dumps(payload).encode("utf-8"),
36-
headers={
37-
"Authorization": f"Bearer {api_key}",
38-
"Content-Type": "application/json",
39-
},
40-
method="POST",
41-
)
42-
4331
try:
44-
with urllib.request.urlopen(request, timeout=60) as response:
45-
body = json.loads(response.read().decode("utf-8"))
46-
except urllib.error.HTTPError as e:
47-
error_body = e.read().decode("utf-8")
32+
response = requests.post(
33+
"https://openrouter.ai/api/v1/chat/completions",
34+
json=payload,
35+
headers={
36+
"Authorization": f"Bearer {api_key}",
37+
"Content-Type": "application/json",
38+
},
39+
timeout=60,
40+
)
41+
response.raise_for_status()
42+
body = response.json()
43+
except requests.exceptions.HTTPError as e:
44+
status_code = e.response.status_code
45+
error_body = e.response.text
4846

49-
if e.code == 401:
47+
if status_code == 401:
5048
raise SystemExit(
5149
"401 Unauthorized: check that claude_cred.txt contains only your LaunchCode-provided course key on a single line."
5250
)
5351

54-
if e.code == 404:
52+
if status_code == 404:
5553
raise SystemExit(
5654
"404 from OpenRouter: your course key is expected to work only with anthropic/claude-haiku-4.5. See example-open-router-responses/example_404_guardrail_error.json."
5755
)
5856

59-
raise SystemExit(f"{e.code} error from OpenRouter:\n{error_body}")
60-
except urllib.error.URLError as e:
57+
raise SystemExit(f"{status_code} error from OpenRouter:\n{error_body}")
58+
except requests.exceptions.RequestException as e:
6159
raise SystemExit(
62-
f"Could not reach OpenRouter: {e.reason}. Check your internet connection and try again."
60+
f"Could not reach OpenRouter: {e}. Check your internet connection and try again."
6361
)
6462

6563
print(body["choices"][0]["message"]["content"])

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests>=2.32,<3

0 commit comments

Comments
 (0)