STAT 133
  1. Tools
  2. OpenRouter
  • Home
  • Syllabus
  • Office Hours

  • Projects
    • Sampson’s Monks
    • Zero Emission Vehicles
    • Your Life in Data
    • Gerrymandering
    • Build a Package
    • Second Opinion

  • Problem Sets
    • PS 1
    • PS 2
    • PS 3
    • PS 4
    • PS 5
    • PS 6
    • PS 7

  • Tools
    • DataHub
    • Shell
    • R
    • R App
    • Git
    • Positron
    • OpenRouter
    • Code Completion
    • Posit Assistant
  1. Tools
  2. OpenRouter

LLM via API

OpenRouter logo

OpenRouter gives you access to hundreds of large language models (LLMs) from many companies through one web app and one API. Some models are paid; others are free up to a daily number of requests. In Stat 133 you use the free models from the shell with curl and from R with ellmer.

To use the API you need an API key: a long secret string that tells OpenRouter who is sending each request. This page covers getting a key and making it available to the shell and to R.

1. Create an account

  1. Go to https://openrouter.ai/ and click Sign in in the upper right.
  2. Choose Continue with GitHub and sign in with the GitHub account you use for this class.

You don’t need to add a credit card. With no credits on your account, you can only use the free models, which is all this class needs.

The OpenRouter home page, with the Get API key button below the headline

2. Create an API key

  1. Click Get API key on the home page, or go to https://openrouter.ai/settings/keys.
  2. Click New key.

The API keys page, with Keys selected in the sidebar and the New key button in the upper right

  1. Fill in the form:
    • Name: something you’ll recognize, like stat133.
    • Expiration: 90 days, so it lasts through the end of the semester.
    • Credit limit: choose Custom amount and enter 0. A key with a $0 limit can only use free models, so it can never cost you anything.
  2. Click Create.

The Create API key form, filled in with the name stat133, an expiration of 90 days, and a custom credit limit of 0

  1. Click the copy button next to your key and paste it somewhere private for now, such as a note on your computer.

The Your new key dialog, showing the start and end of a key that begins sk-or-v1 and a copy button

Warning

The key is shown only once. If you lose it, delete it on the Keys page and create a new one. That’s normal and costs you nothing.

Treat the key like a password. Anyone who has it can send requests as you. Never paste it into a script, a .qmd file, or a repository, and never post it on Ed.

3. Give the key to the shell and to R

Your code shouldn’t contain the key itself. Instead, it reads the key from an environment variable called OPENROUTER_API_KEY. The shell and R each keep their own environment variables, so you set the key in both.

For one session

This is the quickest way, and nothing is saved to disk. You redo it each time you open a new terminal or restart R.

In the shell (Positron’s Terminal pane), run this line with your own key between the quotes:

export OPENROUTER_API_KEY="paste-your-key-here"

The variable lasts until you close that terminal.

At the R console (not in a script, so the key never ends up in a file), run:

Sys.setenv(OPENROUTER_API_KEY = "paste-your-key-here")

The variable lasts until you restart R.

Once and for all (optional)

If you’d rather not paste the key every time, save it in the files that the shell and R read when they start. These files live in your home directory, outside any repository, so the key stays out of your work.

R. At the R console, run:

usethis::edit_r_environ()

That opens a file called .Renviron. Add this line, with your key, and make sure the file ends with a blank line:

OPENROUTER_API_KEY="paste-your-key-here"

Save the file and restart R (Session → Restart R Session in Positron). R reads .Renviron each time it starts.

The shell. Add the same export line to the shell’s startup file:

  • macOS
  • Windows

The startup file is ~/.zshrc. In Terminal, run this with your own key:

echo 'export OPENROUTER_API_KEY="paste-your-key-here"' >> ~/.zshrc

In Git Bash the startup file is ~/.bashrc. In Git Bash, run this with your own key:

echo 'export OPENROUTER_API_KEY="paste-your-key-here"' >> ~/.bashrc

Close the terminal and open a new one. Each new terminal now has the key.

4. Check that it worked

In the shell, ask OpenRouter about your key. This request doesn’t use a model, so it doesn’t count against your free requests.

curl https://openrouter.ai/api/v1/key \
  -H "Authorization: Bearer $OPENROUTER_API_KEY"

If the key is set, the response is a bit of JSON that describes your key, including its limit. If the response is an error with "code":401, OpenRouter didn’t get a working key. "No API key in the Authorization header" means the variable isn’t set in this terminal; "User not found" means the key was mistyped or deleted. Check the variable with:

echo ${#OPENROUTER_API_KEY}

This prints the length of your key (around 70 characters), or 0 if the variable is empty.

In R, check with:

nchar(Sys.getenv("OPENROUTER_API_KEY"))

Then send a first prompt with ellmer:

library(ellmer)

chat <- chat_openrouter(model = "google/gemma-4-31b-it:free")
chat$chat("The capital of Canada is")

Always name a free model, one whose id ends in :free. Without model, chat_openrouter() picks a paid model, and with a $0 limit the request fails.

Tips

  • Free models are limited to a number of requests per day. If you get a 429 error (“rate limited”), wait a bit or try another free model.
  • To find free models, open the Models page on OpenRouter and filter by price.
  • If your key ever ends up somewhere public, delete it on the Keys page right away and create a new one.
 
  • License