Sending JWTs in Authorization Headers
You'll learn how to properly attach JWTs to API requests using the standard Bearer token scheme in Authorization headers.
The Core Idea
Once you've obtained a JWT (whether from localStorage, sessionStorage, or memory), you need to send it with your API requests so the server can verify your identity. The standard way to do this is through the Authorization HTTP request header using the Bearer token scheme. The format is simple:
Authorization: Bearer <your-jwt-token>
Think of it like showing your badge at a security checkpoint—the header is the checkpoint, "Bearer" announces what kind of credential you're presenting, and the JWT is your actual badge.
Why "Bearer"?
The term "Bearer" means "whoever bears (carries) this token has permission to act." It's a standard authentication scheme defined in OAuth 2.0 that tells the server: "I'm presenting a token; trust the token itself, not who I am."
Implementation with Fetch
When making authenticated API requests, include the Authorization header:
const token = localStorage.getItem('jwt'); // or from your storage choice
fetch('https://api.example.com/user/profile', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
Notice the space between "Bearer" and the token—this is required by the specification.
For POST requests with data:
fetch('https://api.example.com/posts', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title: 'My Post' })
});
Key Takeaway: JWTs are sent to APIs using the Authorization: Bearer <token> header format, which is the industry-standard way to present token-based credentials with every authenticated request.