Consuming an API API Keys
Learning objective: By the end of this lesson, students will be able to obtain and API key and understand how to store it safely.
API Keys
An API key is a unique identifier that API providers create to control access to their resources.
The key is usually a long string of characters generated by the API provider for a specific user. When making a request to the API, users are required to provide their key as a query parameter in the URL or as a part of the request body.
Since an API key is a unique identifier, it should be kept secret. If someone else gets a hold of your API key, they can use it to make requests to the API on your behalf. If you are paying for the API, this can result in charges on your account that most companies will hold you accountable for. If you use an API key in your application, you should ensure that it is stored privately and securely.
Specifically, you should never commit it to a GitHub repository. Instead, you should store it as an environment variable in a file that will be ignored when changes are pushed up to GitHub.
Obtaining an API key - Using the OMDb API
For this lesson, we will be exploring the OMDb API. The Open Movie Database (OMDb) API is a web service that provides information about movies, television shows, and actors. It offers a searchable database where you can retrieve data like movie titles, year of release, genres, actors, movie posters, and more. The OMDb API is often used by developers to integrate movie information into their websites or applications.
To use this API, we will need to create an account and request an API key.
Complete the following:
- Navigate to the OMDb API website
- At the top of the page, select the “API Key” link in the navbar
- Select the “FREE!” plan
- Complete the form and submit
Note the information regarding current rate limits listed on this page.
Once you have created your account, your API key will be sent to the email provided during sign up. Check your email for next steps in activating your key.
Activate your API key by following the instructions or link in the email. As noted before, always consult the API creator’s documentation for the most up to date instructions on usage. With third Party API’s, changes can happen at anytime, impacting our interactions and dependency on these external resources.
Keep it secret, keep it safe
Congrats! You have been granted your very first API key. What’s next?
It’s important to keep your OMDb API key secure. Although the most likely risks of someone accessing your key are increased request counts or potential account suspension, we should still treat our API key with care, like a password. It’s all about adhering to best practices and ensuring security. Therefore, we’re going to make sure it’s stored in a safe place.
In a project, this safe place would be in a .env file that is then added to a .gitignore. For now, we’ll just reference it from our email as needed for demo purposes.