Making an API call in Python with an API that requires a bearer token
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A bearer token is usually sent in the Authorization header to prove that the request is allowed to access the API. In Python, the mechanics are simple: build the header, send the request, and handle failures carefully. The parts that actually matter are token safety, error handling, and making sure the token is not hard-coded into the script.
The Basic Request Pattern
The requests library is the usual starting point. A bearer token request looks like this:
The key line is the Authorization header. The API expects the token after the word Bearer.
Parsing JSON Responses Safely
If the API returns JSON, inspect the status first and then decode the body.
raise_for_status() is useful because it turns HTTP error statuses such as 401 or 403 into exceptions immediately.
Do Not Hard-Code the Token
For real code, do not store the token directly in the source file. Use an environment variable instead.
This is safer because the token can be rotated or injected by deployment tooling without changing the codebase.
Sending JSON Data With a Bearer Token
Many APIs require both authentication and a JSON body. In that case, pass the payload with json=.
Using json= is better than manually serializing the body because requests also sets the JSON content type correctly.
Handling Common Failure Cases
Bearer-token APIs often fail in predictable ways:
- '
401 Unauthorizedusually means the token is missing, invalid, or expired' - '
403 Forbiddenusually means the token is valid but lacks permission' - '
400 Bad Requestusually means the payload or query parameters are wrong' - network timeouts usually mean the API is slow or unreachable
A simple error-handling wrapper makes this easier to debug.
When Tokens Expire
Some APIs issue short-lived bearer tokens. In that case, the request code and the token-refresh code are separate concerns. Your request function should use the current token, while another part of the system is responsible for obtaining a fresh one.
That is a design point worth keeping clear. Mixing refresh logic into every request call usually makes the client harder to maintain.
Common Pitfalls
The biggest mistake is forgetting the Bearer prefix in the Authorization header. The token value alone is usually not enough.
Another mistake is hard-coding the token directly into the script or repository. That creates a security problem and makes rotation awkward.
People also forget to set a timeout. Without one, a hung API call can block much longer than expected.
Finally, do not assume 401 and 403 mean the same thing. One usually points to token validity, while the other points to authorization scope.
Summary
- Send bearer tokens in the
Authorizationheader asBearer token_value. - Use
requestswithtimeoutandraise_for_status()for safer API calls. - Prefer environment variables over hard-coded secrets.
- Use
json=when sending JSON payloads. - Treat authentication, authorization, and token-refresh logic as separate concerns.

