Skip to content

LaunchCodeEducation/OpenRouter-API-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenRouter Hello Claude Example

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.

What You Will Learn

  • 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.

What You Need

Python 3

To check whether Python 3 is installed, open a terminal and run:

macOS or Linux:

python3 --version

Windows:

py --version

If 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.

Visual Studio Code

If you do not have Visual Studio Code yet, download it from Visual Studio Code Download.

LaunchCode OpenRouter API Key

Have your LaunchCode-provided OpenRouter API key ready before you begin.

Get This Project On Your Computer

You do not need a GitHub account to complete this tutorial.

Option A: Download ZIP

This is the simplest option if you only need to run the example locally.

  1. Open the OpenRouter-API-Example repository.
  2. Click the green Code button.
  3. Click Download ZIP.
  4. Unzip the downloaded file.
  5. Move it to your Documents folder.
  6. Open the unzipped project folder on your computer using Visual Studio Code.

The unzipped folder may be named OpenRouter-API-Example-main.

Option B: Clone With Git

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

Add Your OpenRouter Key

  1. In the project folder, create a file named claude_cred.txt.
  2. Paste your OpenRouter API key into this file, then save it.
  3. Make sure the file contains only the key, with no quotes, labels, or extra lines.

Open The Terminal In Visual Studio Code

After you open this project folder in Visual Studio Code:

  1. At the top of the Visual Studio Code window, click Terminal in the menu bar.
  2. In the dropdown menu, click New Terminal.
  3. A terminal panel will open at the bottom of the VS Code window.
  4. Make sure the terminal is in this project folder, where hello_claude.py is located.

Verify You Are In The Project Folder

To check your current folder, run the command for your operating system:

macOS or Linux:

pwd
ls

Windows:

pwd
dir

You 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-main

If you used git clone:

cd ~/Documents/OpenRouter-API-Example

You will run the Python command in that VS Code terminal window.

Create And Activate A Virtual Environment

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.

macOS And Linux

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/activate

Windows

Use the default VS Code terminal window.

py -m venv .venv
.\.venv\Scripts\Activate.ps1

When the environment is active, your terminal prompt usually starts with (.venv).

Install The Project Dependency

With the virtual environment activated, run:

macOS And Linux

python3 -m pip install -r requirements.txt

Windows

python -m pip install -r requirements.txt

Run The Example

With the virtual environment activated, run:

macOS And Linux

python3 hello_claude.py

Windows

python hello_claude.py

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

Deactivate The Virtual Environment

When you are done working in this project, run:

deactivate

This returns your terminal to the system Python. If your prompt still starts with (.venv), the environment is still active.

Keep Your Key Safe

  • Do not paste your course key into hello_claude.py or any other Python file.
  • Do not share your API key with other people.
  • Keep claude_cred.txt on your own computer.
  • This repo already lists claude_cred.txt in .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.

Files In This Repo

  • 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.

Common Python Errors

  • Missing claude_cred.txt: If Python says it cannot find claude_cred.txt, make sure that file is in the same folder as hello_claude.py.
  • ModuleNotFoundError: No module named 'requests': Your virtual environment is not activated yet, or you have not run the pip install -r requirements.txt command for your operating system.
  • 401 Unauthorized: claude_cred.txt contains 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 only anthropic/claude-haiku-4.5.
  • 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.
  • Could not reach OpenRouter: Check your internet connection and try again.
  • SSL certificate errors: These are less common with the requests library, 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.bat instead.

About

Hello World tutorial for using a LaunchCode-provided OpenRouter API key to access Claude from Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages