JWT Generator · Tutorial

How to create a test JWT for a local API

7 min readintermediateUpdated

You're developing an API that checks JWTs, and you need tokens with different users, roles and expiry times to test it without going through a full sign-in each time. This tutorial creates an HS256 token signed with your development secret, and shows how to send it and how to test the expired case.

What you need

  • The JWT Generator
  • Your API's development secret and expected claims (the Try it button loads a sample using the secret bentools-demo-secret)
  • A way to send requests, such as curl or an API client

Steps

  1. Choose the algorithm

    Select HS256, the default for many frameworks that use a shared secret. If your API is configured for RS256 or ES256, choose that instead, and you'll need a private key rather than a secret.

  2. Write the claims your API checks

    Edit the payload JSON. Include whatever your API reads, typically sub (the user ID), a role or scope, and aud or iss if your API validates them. The sample uses sub, name and role.

  3. Set the timestamps

    Press Set iat to now and Expire in 1 hour. They write the current Unix time and one hour later into iat and exp, so the token is valid right away.

  4. Enter the secret and sign

    Type your development secret into Secret. It must match the API's configuration exactly, including case and spaces. Press Create token. The token appears below, ready to copy.

  5. Call your API

    Send the token in the Authorization header. A 200 response means your API accepted it.

    curl -H "Authorization: Bearer <paste token here>" http://localhost:3000/api/orders
  6. Test the failure cases

    Change exp to a time in the past and sign again. Your API should now return 401. Then change the role to something unauthorised and check you get 403. Testing these cases catches security bugs early.

Try it yourself

Opens the JWT Generator with this tutorial's sample loaded.

Try it now

Common mistakes

A secret that doesn't match
If every token is rejected, compare the secret character by character. Some frameworks expect the secret Base64-encoded, others as plain text.
Timestamps in milliseconds
JWT times are in seconds. A value like 1793350800000 (milliseconds) is thousands of years in the future and may be rejected or treated as never expiring.
Using production secrets
Use a separate secret for development. If a production secret appears in a browser, a test script or a chat, rotate it.

Next steps

Go back to the JWT Generator, or keep going with one of these:

FAQ

Can I add custom claims?

Yes. Any JSON is allowed in the payload. Keep it small, because the token is sent with every request.