JWT Authentication & Authorization
Simulate secure authentication flows with JWT tokens for realistic API testing
What is JWT Authentication?
JWT (JSON Web Token) is a secure, compact way to authenticate API requests. Mock API Builder simulates real JWT authentication flows, allowing you to test your application's authentication logic without setting up a backend.
Login, receive a token, include it in requests—just like production APIs
Tokens expire after 7 days by default, simulating real-world security
Mark endpoints as protected—requires valid JWT to access
How JWT Authentication Works
User Logs In
POST credentials to the /auth/login endpoint
Receive JWT Token
Server returns a signed JWT token that expires in 7 days
Store Token
Client stores token securely (localStorage, sessionStorage, or cookies)
Include in Requests
Add token to Authorization header for protected endpoints
Verification
Server validates token and grants or denies access
Login Endpoint
Request Body:
Success Response (200):
Error Response (401):
Mock Credentials
For testing, you can use any email/password combination. The mock API will accept any credentials and return a valid token.
💡 In production, you would validate against a real user database.
Using JWT Tokens in Requests
Include the JWT token in the Authorization header using the Bearer scheme:
cURL Example:
JavaScript fetch():
Axios Example:
Protecting Endpoints
Mark endpoints as "protected" to require JWT authentication:
Step-by-Step:
- 1. Navigate to your endpoint in the project dashboard
- 2. Click "Edit Endpoint"
- 3. Toggle "Require Authentication"
- 4. Save changes
What Happens:
- • Requests without a token →
401 Unauthorized - • Requests with invalid token →
401 Unauthorized - • Requests with expired token →
401 Token Expired - • Requests with valid token →
200 OK(or appropriate status)
JWT Token Structure
A JWT consists of three parts separated by dots:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzEyMyIsImVtYWlsIjoidXNlckBleGFtcGxlLmNvbSJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5cPart 1: Header
Contains the token type (JWT) and hashing algorithm (HS256)
Part 2: Payload
Contains the user data and claims
Part 3: Signature
Verifies the token hasn't been tampered with
Complete Authentication Example
Common Use Cases
Build and test login flows for SaaS applications, admin panels, or user portals before connecting to a real authentication system.
Test authentication in iOS and Android apps without waiting for backend APIs. Store tokens securely using platform-specific storage.
Verify your app handles expired tokens correctly by testing with tokens that are set to expire soon. Ensure users are redirected to login when tokens expire.
Test different user roles (admin, user, guest) by including role information in the JWT payload and enforcing permissions on the frontend.
Best Practices
- ✓Store tokens securely using httpOnly cookies when possible, or secure storage APIs
- ✓Always use HTTPS in production to prevent token interception
- ✓Handle token expiration gracefully by redirecting users to login
- ✓Include tokens in Authorization header rather than URL parameters
- ✓Clear tokens on logout to prevent unauthorized access
- ✓Validate tokens on every protected request to ensure they're still valid
- ✓Test both success and failure scenarios including invalid credentials and expired tokens