You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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.
@@ -4,7 +4,7 @@ This repo is a beginner-friendly example of how to send a simple Claude request
4
4
5
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
6
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`libraryto make the OpenRouter call.
8
8
9
9
## What You Will Learn
10
10
@@ -16,7 +16,30 @@ This tutorial uses only Python's standard library, so you do not need to install
16
16
## What You Need
17
17
18
18
- 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.
20
43
21
44
## Get This Project On Your Computer
22
45
@@ -57,35 +80,136 @@ Do not add quotes, labels, or extra lines.
57
80
58
81
After you open this project folder in Visual Studio Code:
59
82
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**.
62
85
3. A terminal panel will open at the bottom of the VS Code window.
63
86
4. Make sure the terminal is in this project folder, where `hello_claude.py` is located.
64
87
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
+
65
120
You will run the Python command in that VS Code terminal window.
66
121
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.
68
127
69
128
Use the terminal window inside Visual Studio Code.
70
129
71
130
### macOS
72
131
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.
74
133
75
134
```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
77
144
```
78
145
79
146
### Windows
80
147
81
-
In the VS Code terminal, run:
148
+
Use the default VS Code terminal window.
82
149
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
85
201
```
86
202
87
203
If everything is set up correctly, the script prints a short reply from Claude.
88
204
205
+
## Deactivate The Virtual Environment
206
+
207
+
When you are done working in this project, run:
208
+
209
+
```bash
210
+
deactivate
211
+
```
212
+
89
213
## Keep Your Key Safe
90
214
91
215
- 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
100
224
## Files In This Repo
101
225
102
226
-`hello_claude.py`: the minimal Python example for the course-approved OpenRouter call.
227
+
-`requirements.txt`: the Python dependency list for this project.
103
228
-`example-open-router-responses/successful_call_example.json`: an example JSON response from a successful request.
104
229
-`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.
105
230
-`example-open-router-responses/claude-haiku-4.5_model-details.json`: reference details for the course-approved model from OpenRouter.
106
231
107
232
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.
108
233
109
-
## Troubleshooting
234
+
## Common Python Errors
110
235
111
236
- 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`.
112
238
-`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.
115
241
- 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.
"401 Unauthorized: check that claude_cred.txt contains only your LaunchCode-provided course key on a single line."
52
50
)
53
51
54
-
ife.code==404:
52
+
ifstatus_code==404:
55
53
raiseSystemExit(
56
54
"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."
57
55
)
58
56
59
-
raiseSystemExit(f"{e.code} error from OpenRouter:\n{error_body}")
60
-
excepturllib.error.URLErrorase:
57
+
raiseSystemExit(f"{status_code} error from OpenRouter:\n{error_body}")
58
+
exceptrequests.exceptions.RequestExceptionase:
61
59
raiseSystemExit(
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."
0 commit comments