Loading video player…

Getting Started With OpenRouter

Resource mentioned in this lesson: OpenRouter API documentation

00:00 Why use OpenRouter? Well, the AI ecosystem is fragmented. Different AI providers expose different APIs. Across those providers, the authentication schemes are going to vary, as will rules around rate limits and token use.

00:14 Not to mention, model availability is changing constantly. All of this together makes developing for AI complex and time-consuming. What OpenRouter does is provide you with a unified API across providers.

00:27 Using OpenRouter, you can access hundreds of AI models, switch between different model providers without changing your code, compare the outputs of multiple models, all of which helps you build more resilient AI applications. So to get started, you’ll need an API key.

00:44 So head on over to openrouter.ai and create an account. Once you’ve created an account, you can get an API key. In the future, this probably looks different, but it’s most likely still pretty straightforward.

00:55 You can follow the big bold Get API Key button, or you can navigate to Workspaces and get your keys from there.

01:04 Workspaces and View your API Keys.

01:08 From here, hit Create.

01:12 Give your API key a name.

01:16 Don’t worry about credit limit or reset limit for now, since the free plan will be more than enough for this course. I’ll be using an expiration for obvious reasons, but you don’t need to worry about that either.

01:27 Click Create.

01:29 And now that you’ve got your API key, you’ll need to save it somewhere secure, because otherwise you won’t be able to get it again. Now that you’ve got your API key, open up your IDE to get started.

01:41 I’m here in a folder called OPENROUTER-PROJECT, and since you’ll need to install the requests package, you’ll want to have a virtual environment active here.

01:48 You can create one with python -m venv venv, and activate it with source venv/bin/activate.

01:58 And then install requests, python -m pip install requests.

02:05 And importantly, export your API key to the variable OPENROUTER_API_KEY.

02:13 Once you’ve done that, take a look at get_models.py, which you can find in the course resources, along with all of the other scripts you’ll be using throughout this course. This short script will validate that your API key is working.

02:26 It imports os and requests, defines the OpenRouter models URL endpoint, and collects the OpenRouter API key you just exported. That API key is included in the headers dictionary.

02:37 That’s how you authenticate with the OpenRouter API. Then, you make a GET request to the API endpoint, passing in the headers, and save the response as a dictionary by calling the .json() method.

02:48 Get data from the data dict, returning an empty dictionary if there is no data key. That’s error handling. And finally, the script prints out the results of your query with some nicely formatted f-strings.

03:00 Now, this is important. Run the script from the same shell you used to export the API key. python get_models.py.

03:09 Success! Found 370 models via OpenRouter, followed by a list of some of the available models. Congratulations! You just made your first query with OpenRouter.

03:19 Most likely, the output you’ll see here will differ as time moves on. But what’s important is that you see the success message. If you do run into any trouble, first confirm your API key is correct and valid.

03:31 Then verify that the exported variable name is OPENROUTER_API_KEY exactly or else it won’t work. And if you’re still stumped, review the OpenRouter documentation at openrouter.ai/docs.

03:45 And in the next lesson, you’ll query an actual model. See you there.

Become a Member to join the conversation.