Insomnia: Signing In and Sending a Token

Before you secure the categories API, save the requests a signed-in client sends in Insomnia, register, log in, and send the token from the login's answer as a Bearer token, and see that today the API lets anyone in.

Journeyman · REST API · ASP.NET Core · Updated

After this lesson you can

  • Sign in from Insomnia and send the token from the login's answer as a Bearer token

Key takeaways

  • A client signs in once, keeps the token from the answer, and sends it with every request after that, in the Authorization header as a Bearer token.
  • In Insomnia, the Auth tab's Bearer Token can take its value from another request's answer, so a new login gives every request the new token by itself.
  • 401 Unauthorized means "who are you?": no token, or a bad one. 403 Forbidden means "I know who you are, and you can't do this".
  • Today the API answers 201 to anyone. The answer you want without a token is 401.

Send Add Snacks from your Northwind API collection in Insomnia. It answers 201 Created. So does anyone else's: your API adds a category for whoever asks, and it would delete one too. In the next lesson the API gets users, and decides who may do what. As always, start by writing down what a client will send, and what you want each request to answer.

JSON Web Tokens explained the plan: a user signs in once, and gets a token back, like a wristband at a concert. After that, they show the wristband with every request, instead of signing in again.

Signing up and signing in

Add two requests to the Northwind API collection. Both are POSTs with a JSON body (the Body tab, then JSON), and both send the same user:

Register ada:

POST {{ _.base_url }}/api/auth/register

Log in:

POST {{ _.base_url }}/api/auth/login

The body for both:

{ "userName": "ada", "password": "summer2026" }

Send them. Both answer 404 Not Found: nothing answers at /api/auth yet. Here is what you want Log in to answer once the next lesson is built, 200 OK and a token:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "userName": "ada",
  "role": "User"
}

The token is much longer than this, and different every time. It's the wristband.

Add one more, Log in, wrong password: duplicate Log in (right-click it, Duplicate) and change the password to summer2025. The answer you want is 401 Unauthorized.

Showing the wristband

Every request that needs a signed-in user sends the token in a header:

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Bearer means "whoever holds this": like a concert wristband, the token works for whoever shows it, so keep it to yourself.

You could copy the token out of Log in's answer and paste it into each request. But it changes every time you log in, and pasting it into ten requests gets old fast. Insomnia can fetch it for you instead. Add a request, Who am I:

GET {{ _.base_url }}/api/auth/me

Then give it the token:

  1. Open its Auth tab, and choose Bearer Token.
  2. Click in the Token field and press Ctrl+Space. Choose Response ⇒ Body Attribute. A colored tag appears in the field.
  3. Click the tag to set it up: Request is Log in, and Filter is $.token, which means "the token from the answer's body".

Now Who am I sends whatever token Log in last answered with. Log in again, and every request with that tag uses the new token.

Send Who am I. Today it's 404 Not Found, like the others. The answer you want is 200 OK, with {"userName":"ada","role":"User"}.

Who may do what

Now the requests that change data. Duplicate Add Snacks, name the copy Add Snacks, signed in, and give it the same Bearer token tag on its Auth tab. Do the same with Delete Snacks, as Delete Snacks, as ada, and change its address to the id of a Snacks that still exists: the last answer of Add Snacks, signed in says it.

Request Today The answer you want
Add Snacks, with no token 201 Created 401 Unauthorized: "who are you?"
Add Snacks, signed in, with ada's token 201 Created 201 Created: any signed-in user may add
Delete Snacks, as ada, with ada's token 204 No Content 403 Forbidden: "I know who you are, and you can't do this": only an admin may delete
Register ada, a second time 404 Not Found 409 Conflict: the name is taken

The first row is the one that matters most. Today, Add Snacks works for anyone. After the next lesson, the same request with no token is turned away.

401 and 403 sound alike, and mean different things. 401 Unauthorized is about who you are: there's no token, or it's expired, changed or not one the API made. 403 Forbidden is about what you may do: the API knows who you are, and your role doesn't allow it.

Check yourself

2 questions to try on the lesson page.

So what

Your collection now holds the whole signed-in conversation: register, log in, and every request after that showing the token. In the next lesson you build the API's side of it, and send these requests again until each one answers what you wrote down.

Key terms

Cheat sheets