API key
An API key is a long, random string that a service hands out so a program can identify itself when it calls that service’s API. The client sends the key along with every request, and the provider uses it to decide whether to answer, which account to bill, and how many calls to allow under its rate limit.
Most providers expect the key in an HTTP header, although some still accept it as a query parameter in the URL. A request to a payment API looks something like this, with the key carried in the Authorization header:
POST /v1/refunds HTTP/1.1
Host: api.payments.example
Authorization: Bearer sk_live_51Hx8kQ2eZvKYlo2C9fT3a
Content-Type: application/x-www-form-urlencoded
charge=ch_3PqL2x&amount=1500
An API key identifies a project or an application rather than a person. Google Cloud’s documentation spells this out: a standard key associates a request with a project for billing and quota purposes but doesn’t authenticate a user. Anyone holding the key can make the same calls the key allows.
How It Shows Up in Practice
A Python developer usually meets an API key on a provider’s dashboard, right after signing up for a weather, payment, or AI model service. The dashboard shows the key once, and the next question is where to put it so the code can read it without anyone else seeing it.
The common answer is an environment variable. The code reads the value with os.environ at startup, and the key itself lives outside the repository, often in a local .env file that’s listed in .gitignore. This follows the twelve-factor app rule of keeping configuration in the environment instead of in the code.
Production systems take this a step further and load keys from a secrets manager, such as HashiCorp Vault or AWS Secrets Manager. Each environment gets its own key, so the staging server never holds the key that moves real money.
Many providers prefix their keys so their purpose is obvious at a glance. Stripe, for example, issues secret keys that start with sk_live_ for real payments and sk_test_ for its sandbox. The prefix also makes a leaked key easier for scanning tools to recognize.
Common Pitfalls
The classic mistake is committing a key to version control. Once it’s in the Git history, deleting the line in a later commit doesn’t remove it, and bots scrape public repositories for exactly this kind of string. GitHub’s secret scanning watches for known key formats, and its push protection can block a push that contains one before it lands.
A leaked key gets treated as an incident. The fix is to rotate it, which means revoking the old key and issuing a replacement, and then to check the provider’s logs for calls nobody on the team made.
Teams limit the damage in advance by giving each key the narrowest permissions the code needs. Restricted keys that can only read data, or that only work from known server IP addresses, limit what an attacker can do with a leaked key. Deciding which keys exist and what each one may do is a regular part of threat modeling.
Related Resources
Course
Interacting With REST APIs and Python
In this video course, you'll learn how to use Python to communicate with REST APIs. You'll learn about REST architecture and how to use the requests library to get data from a REST API. You'll also explore different Python tools you can use to build REST APIs.
For additional information on related topics, take a look at the following resources:
- How to Use the Claude API in Python (Tutorial)
- Generate Images in Python With OpenAI's GPT Image API (Tutorial)
- Using the Claude API in Python (Course)
- Building HTTP APIs With Django REST Framework (Course)
- Python REST APIs With FastAPI (Course)
- Sneaky REST APIs With Django Ninja (Course)
- How to Use the Claude API in Python (Quiz)
- Generate Images in Python With OpenAI's GPT Image API (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Nov. 11, 2026