JWT auth for your API without code: a template that issues and checks tokens

JWT auth for your API without code starts in nana800 with a ready-made template in two parts: the first webhook issues a signed token, the second checks the token from a request and answers with the result. You need no server and no libraries, and tokens are signed with HS512 using your secret key. Your balance is topped up to $3.50 for free every month, and no card is needed to start.

Build it in the builder
The JWT issue and check (Jwt Token Auth) template scheme in the nana800 builder

Chain of blocks

  1. 1

    Trigger

    button issues a token by hand, without a request, which is handy for checking the settings of the sign jwt block. The button is free.

  2. 2

    Trigger 路 Webhook (trigger)

    webhook for issuing receives the token request. In the template it uses the GET method and the "/" path, so set your own path, for example /token.

  3. 3

    Action 路 Issue JWT token

    sign jwt signs the token with HS512. You fill in the secret key, the token owner (sub), the data inside the token and its lifetime. The block returns token and the expiry time expires_at.

  4. 4

    Action 路 Webhook response

    webhook response sends the issued token back to the sender. The response body is empty in the template: put an object with the token into body json.

  5. 5

    Trigger 路 Webhook (trigger)

    webhook for checking receives a request with a token. It also uses GET and the "/" path, so it needs its own path, for example /verify, or requests never reach it.

  6. 6

    Action 路 Verify JWT token

    validate jwt checks the signature and expiry of the token with the same secret key. It returns valid (true or false), the token data in payload and the reason for a refusal in error_text.

  7. 7

    Action 路 Webhook response

    webhook response answers with the check result. Its body is empty too: return valid and payload from the check block.

What it is good for

  • Sign-in for a mobile app or a customer account: issue a token after checking the login, then let requests in only with it
  • Closing your own API built on nana800 webhooks to outside requests
  • Temporary access for a partner or contractor for a week or a month
  • Checking tokens that your own server signed with HS512 and the same key
  • Passing a user ID and role between systems inside a signed token

How to set it up

  1. 1

    Create a scheme from the template

    In the builder open Templates, find the Jwt Token Auth template and click Use. Choose the project and scheme and click Create: both parts appear, token issuing and token checking.

  2. 2

    Give the webhooks different paths

    In the first webhook block pick the POST method and the /token path, in the second one POST and /verify. Both use the "/" path in the template, and while the paths are the same, the second webhook gets no requests.

  3. 3

    Set up token issuing

    In the sign jwt block paste the secret key: a long random string of at least 32 characters. Insert the user ID from the request body into the owner (sub) field and set the lifetime, for example 7d. In the next response block put an object with a token field into body json and insert token from the sign jwt block.

  4. 4

    Set up token checking

    In the validate jwt block insert the token from the request into the jwt token field, for example the token field of the body, and paste the same key as in sign jwt into jwt secret. In the last response block return valid and payload from the check block.

  5. 5

    Turn the scheme on and test it

    Turn the scheme on, then press the button in the button trigger and see the token in the result of the sign jwt block in the run history. The response block shows an error because nobody waits for the answer, which is expected. Next click the API button in the builder, request a token on the Try it tab and send it to /verify. While the scheme is on, it costs $0.0042 per hour.

Services in this template

  • Webhook

    Receive website leads and service events with a webhook and answer requests with no server and no code. Free balance every month, no card needed.

  • JWT

    Issue and verify JWT tokens without code: token login for your own API, token lifetime and your own data inside. Free balance every month, no card needed.

Frequently asked questions

Issuing a token takes two basic block runs: the webhook and sign jwt. Checking also takes two: the webhook and validate jwt. Answers are free, and 1,000 basic block runs cost $1. An enabled scheme costs $0.0042 per hour. Your balance is topped up to $3.50 for free every month, no card needed. A scheme kept on around the clock for a month spends most of that on scheme hours.

No. In the template anyone who calls the issuing address gets a token. Add a check before the sign jwt block: look the user up in your database with the PostgreSQL, MySQL, MongoDB or built-in nana800 database block, and let only a match through with the free condition block.

The validate jwt block returns valid: false with the reason in error_text, the scheme keeps going, and the response block answers with the code chosen in it. To answer a bad token with code 401, put the free condition block after the check and connect two different response blocks to its two exits.

HS512: the token is signed and checked with one shared secret key. Tokens with other algorithms, such as RS256 with a key pair, are not accepted by the validate jwt block. Keep the key secret: anyone who has it can issue tokens that pass your check.

The simplest way is a body field such as token, or a header of your own. If the client sends an Authorization header with the word Bearer before the token, the check block needs the token without that word: cut it off with the free run custom script block.

No. Anyone who has the token can read what is inside, and the signature only protects it from forgery. Put a user ID, a role and other data that is safe to show into the token data (JSON) field.

Build your first scheme for free

Sign-up is free and needs no card. Every month the balance is topped up to $3.50 for free, enough for about 3,500 runs of basic blocks.

Build it in the builder