Skip to main content

How to Get an Access Token for the Apache Airflow® REST API

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Apache Airflow® 3 has a REST API for everything the UI does: list Dags, trigger runs, read logs. Most of its endpoints refuse a request that doesn't prove who's asking. With the simple auth manager that airflow standalone uses, the proof is a JWT access token, which you get by posting a username and password to /auth/token. This guide gets one and uses it, on Airflow 3.4.0.

Syntax​

Get a token:

Syntax
curl -X POST <airflow URL>/auth/token \
-H 'Content-Type: application/json' \
-d '{"username": "<username>", "password": "<password>"}'

Use it:

Syntax
curl <airflow URL>/api/v2/<endpoint> -H "Authorization: Bearer <token>"
PartWhat it means
<airflow URL>Where the API server runs, such as http://localhost:8080
/auth/tokenThe simple auth manager's endpoint that creates tokens
-d '{…}'The username and password, as JSON
/api/v2/<endpoint>Any REST API endpoint, such as /api/v2/pools
Authorization: Bearer <token>The header that carries the token

Airflow's guide to generating a token with the simple auth manager uses exactly this curl -X 'POST' request, and says it returns a token "that you can use in the Airflow public API requests".

Prerequisites​

  • Apache Airflow® running with the simple auth manager, which airflow standalone uses.
  • The admin password, from simple_auth_manager_passwords.json.generated in your AIRFLOW_HOME.

Step 1: Request a Token​

My API server is on http://127.0.0.1:8085. Put your own password in place of <password>:

Terminal
curl -s -X POST http://127.0.0.1:8085/auth/token \
-H 'Content-Type: application/json' \
-d '{"username": "admin", "password": "<password>"}'
Output
{"access_token":"eyJ...(masked)"}

The terminal: the POST to /auth/token returns JSON with an access_token that starts with eyJ, the rest masked.

The answer is one JSON field, access_token. Its value is a JWT, a long string that starts with eyJ; I masked mine, because anyone holding it can use the API as admin until it expires. -s hides curl's progress meter.

Step 2: Call the API Without It​

Terminal
curl -s http://127.0.0.1:8085/api/v2/pools
Output
{"detail":"Not authenticated"}

The API refuses: no token, no data.

Step 3: Call It With the Token​

The quickest way is to keep the token in a variable. This line asks for a token and keeps only its value:

Terminal
TOKEN=$(curl -s -X POST http://127.0.0.1:8085/auth/token \
-H 'Content-Type: application/json' \
-d '{"username": "admin", "password": "<password>"}' \
| python3 -c 'import json,sys; print(json.load(sys.stdin)["access_token"])')
curl -s http://127.0.0.1:8085/api/v2/pools -H "Authorization: Bearer $TOKEN"
Output (start)
{"pools":[{"name":"default_pool","slots":128,"description":"Default pool","include_deferred":false,"occupied_slots":0,"running_slots":0,"queued_slots":0,"scheduled_slots":0,"open_slots":128,"deferred_slots":0,"team_name"...

The terminal: without a token, /api/v2/pools answers Not authenticated; with the Authorization Bearer header, it returns the default_pool with 128 slots.

With the token, the same request returns Airflow's pools: default_pool, with 128 slots. The small Python program in the first command reads the JSON answer and prints only access_token.

Common Mistakes​

MistakeWhat happensDo this instead
Testing with /api/v2/versionIt answers without a token, so it proves nothingUse an endpoint such as /api/v2/pools
Pasting a token into a post, chat or ticketAnyone can use the API as you until it expiresTreat it like a password

Troubleshooting​

SymptomLikely causeFix
{"detail":"Not authenticated"}No Authorization header, or no token in itAdd -H "Authorization: Bearer $TOKEN"
An empty TOKEN variableThe token request's answer had no access_tokenRun the first curl alone and read its answer

FAQ​

How long is a token valid?​

24 hours by default: [api_auth] jwt_expiration_time is 86400 seconds.

Does this work with other auth managers?​

This endpoint is the simple auth manager's. Other auth managers have their own way of issuing tokens.

Why do I see a warning about the HMAC key in Airflow's output?​

Airflow signs tokens with a generated key that's shorter than recommended; Fixed: The HMAC key is 24 bytes long shows the fix.

Conclusion​

A POST to /auth/token with the admin's username and password returned an access_token. Without it, /api/v2/pools answered Not authenticated; with Authorization: Bearer and the token, it returned the default_pool.

Additional Resources​

Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.