What is JWT and How Does it Work?

JSON Web Tokens (JWTs) are a compact, URL-safe means of representing claims to be transferred between two parties. These claims are often used to ensure the integrity and authenticity of information.

David Demir

David Demir

17 May 2025

What is JWT and How Does it Work?

JSON Web Tokens (JWTs) are a compact, URL-safe means of representing claims to be transferred between two parties. These claims are often used to ensure the integrity and authenticity of information.

This article explores how JWTs work. We will look at the structure of a JWT, which consists of three encoded and digitally signed parts. We will then discuss common use cases for JWTs and review some of the benefits of using them,

The Structure of a JWT(With Example)

A JSON Web Token (JWT) consists of three parts separated by dots: Header, Payload, and Signature. The overall structure is:

xxxxxxxxxx.yyyyyyyyyy.zzzzzzzzzz

Payload:

Signature:

Putting it all together:

Header: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
Payload: eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
Signature: HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)

Encoded JWT:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwia

The Benefits of using JWT:

JWTs contain JSON objects that are encoded and signed, ensuring integrity and authenticity without relying on stateful sessions on the server.

Some of the key benefits that make JWTs useful are:

JWT vs OAuth: the Difference between JWT and OAuth

JWT (JSON Web Token) and OAuth are related but serve different purposes in the context of authentication and authorization in web development. Here's a brief comparison of JWT and OAuth:

JWT (JSON Web Token):

  1. Purpose: JWT is a compact, self-contained way to transmit information between parties securely. It is commonly used for authentication and information exchange.
  2. Content: JWT contains claims about an entity (typically, a user) and is often used for user authentication. Claims can include user ID, roles, permissions, and other relevant information. JWTs are often used to create stateless authentication systems.
  3. Usage: JWTs are usually sent as part of the request headers in HTTP requests. They are used to represent the identity and privileges of the user.

OAuth (Open Authorization):

  1. Purpose: OAuth is a framework for authorization, allowing a third-party application to access a user's resources on another server without exposing the user's credentials. OAuth is not designed for authentication but for delegation of authority.
  2. Roles: OAuth defines roles such as Resource Owner, Client, Authorization Server, and Resource Server. It enables scenarios where a user can grant access to their resources (e.g., photos, contacts) to another application without sharing their credentials.
  3. Grant Types: OAuth introduces grant types (authorization code, implicit, client credentials, password, etc.) to specify how the authorization process should be carried out.
  4. Tokens: OAuth involves the use of access tokens and refresh tokens. Access tokens are used to access protected resources, and refresh tokens can be used to obtain a new access token.

Key Differences:

How Does JWT Work?

JSON Web Tokens (JWTs) play a key role in authentication by providing a secure and efficient way to verify the identity of users. The process typically involves the following steps:

  1. User Authentication: Users sign in using their username and password or through external providers like Google or Facebook. The authentication server validates the credentials and issues a JSON Web Token (JWT) certifying the user's identity.
  2. JWT Generation: The identity provider (IdP) generates a JWT, signing it with either a secret salt or a private key. This JWT encapsulates essential user information, such as user ID, roles, and expiration time.
  3. JWT Usage: The user's client securely stores the JWT obtained during authentication. When accessing protected resources, the client includes the JWT in the HTTP Authorization header.
  4. Token Verification: The resource server decodes and verifies the authenticity of the JWT using the provided secret salt or public key. This process ensures the integrity and origin of the token.

JWT in Apidog

Apidog is a user-friendly API development and testing tool that excels in managing JSON Web Tokens (JWT). With its intuitive interface, Apidog simplifies the process of handling JWTs, providing automatic support for token generation, dynamic management, and seamless inclusion in API requests.

JWT

This tool streamlines the JWT-related aspects of API development, allowing developers to focus on efficient testing and integration within their workflows.

button

Explore more

A Beginner's Guide for Google MCP Toolbox for Databases

A Beginner's Guide for Google MCP Toolbox for Databases

An Introduction to the MCP Toolbox The MCP Toolbox is a free tool from Google that helps your AI applications talk to your databases. Think of it as a special translator. Your AI can ask for information in a simple way, and the MCP Toolbox translates that request into the language your database understands, like SQL. It uses something called the Model Context Protocol (MCP), which is just a standard set of rules for this kind of communication. 💡Want a great API Testing tool that generates bea

7 July 2025

Google Gemini API Batch Mode is Here and 50% Cheaper

Google Gemini API Batch Mode is Here and 50% Cheaper

Google's Gemini API now features Batch Mode, a transformative update designed for large-scale, asynchronous tasks that comes with a 50% reduction in cost. 🚀 * This powerful endpoint allows you to process enormous jobs with your results delivered within 24 hours, all at half the standard API price. * The system is engineered for high-throughput workloads, accommodating up to 2GB JSONL files and leveraging optimizations like Context Caching for greater efficiency. * It also supports built-in

7 July 2025

Top 10 Documentation Site Generator for Developers in 2025

Top 10 Documentation Site Generator for Developers in 2025

In the world of software development, clear, concise, and accessible documentation is no longer a mere accessory; it is the bedrock of a successful project. Well-crafted documentation empowers developers to understand and effectively utilize APIs, frameworks, and libraries, fostering a vibrant and collaborative ecosystem. As we venture further into 2025, the tools available to generate these crucial resources have evolved, becoming more intelligent, collaborative, and deeply integrated into the

7 July 2025

Practice API Design-first in Apidog

Discover an easier way to build and use APIs