Blog

A request chain with authorization: token and refresh_token

The staging token lives for 15 minutes. Every catalog check after a break starts the same way: 401, the sign-in request, copying the token into the header, again. A request chain in Microkoi Bench signs in by itself, keeps the token between runs and refreshes it with refresh_token when it’s about to expire.

Why signing in by hand eats the check

You can’t check a protected endpoint with one request. First sign in, then put the token into the Authorization header, then send the request itself. Fifteen minutes later the token has expired, and it all starts over.

It gets worse when sign-in opens every check. The authorization server limits sign-ins, and on the tenth run in a row you get 429 instead of the catalog. And the token refresh flow, which the app has dedicated code for, never gets tested at all.

What you need in the collection

The example is a marketplace catalog. The collection has four requests: sign-in, token refresh, the product list and a product page. The server address is the baseUrl variable in the “Staging” environment.

RequestMethod and address
Sign inPOST {{baseUrl}}/auth/sign-in
Refresh tokenPOST {{baseUrl}}/auth/refresh
CatalogGET {{baseUrl}}/catalog?category=shoes
ProductGET {{baseUrl}}/products/{{productId}}

Step 1. Take the tokens from the sign-in response

Send “Sign in” from its request tab. The staging response looks like this:

Sign-in response, 200 OK
{
  "data": {
    "access_token": "eyJhbGciOiJIUzI1NiJ9.stage",
    "refresh_token": "rt-7f3c9a2e41d8",
    "expires_in": 900
  }
}
  1. In the response, open Body → “Model”.
  2. Hover over access_token and click “Save to variable”. An “After response” rule appears with the variable token.
  3. Save refresh_token and expires_in the same way: they become refreshToken and expiresIn.
  4. Save the request.

Bench marks token fields as secrets right away: history and reports show dots instead of the value.

Step 2. Put the tokens into the requests

On the “Auth” tab of “Catalog” and “Product”, choose “Bearer token” with the value {{token}}. The body of “Refresh token” takes the refresh token from a variable:

Refresh token request body
{
  "refresh_token": "{{refreshToken}}"
}

Add one more “After response” rule to “Catalog”: path data.items[0].id, variable productId. “Product” uses it.

Step 3. Build the chain

  1. Click the arrow next to the “Request” button above the tree and choose “New chain”. Type a name, for example “Catalog check”, and press Enter.
  2. Click “Add step”, find “Catalog” and press Enter. Add “Product” the same way.
  3. Press Esc to deselect the step: the chain properties appear on the right.
  4. In the “Authorization” section, click “Sign in with a token…” and choose “Sign in”.
  5. In the block properties, in the “Refresh” field, choose “Refresh token”.

An “Authorization” block with a key icon appears above the steps. Bench fills in the “Token”, “Refresh token” and “Expires” paths itself: the sign-in request already has rules for access_token, refresh_token and expires_in. The block shows its rule: “token, refreshToken · refresh on 401 or before expiry”.

Sign-in isn’t a chain step. If “Sign in” was already in the chain as a step, Bench removes the step and shows a “Step removed.” bar with an “Undo” button.

Step 4. Run it and read the block’s status line

Choose the “Staging” environment and click “Run” (⌘↩, or Ctrl+Enter on Windows and Linux). The first run signs in, the next ones use the saved token. The status line of the “Authorization” block tells you what happened:

What the block showsWhat happened
“Signed in”There was no saved token, so Bench signed in
“Token reused”The token came from the previous run, no sign-in or refresh needed
“Token refreshed”The token was about to expire, Bench refreshed it with refresh_token
“Signed in again”The refresh token had expired too, so Bench signed in again
“Couldn’t sign in”Sign-in failed, and the steps didn’t run

The token is refreshed 60 seconds before the expiry from expires_in. If the server revoked the token earlier and a step got 401, Bench refreshes the token and retries the step once. Under the step’s status line you’ll see “401 → token refreshed → retried” and the retry’s status code.

Test the refresh flow on purpose

The app’s token refresh code runs once every 15 minutes, so it breaks quietly. A chain checks the server side of it without waiting around.

  • Run the chain twice, 15 minutes apart: the block shows “Token refreshed”, and “Refresh” has a 200.
  • If the refresh returned 401, the block shows “Signed in again” and a yellow line, “Refresh token expired, used Sign in”. That means the refresh token on staging lives shorter than the app expects.
  • “Forget token” in the block properties deletes the saved tokens. The next run starts with sign-in again — that’s how you check the first sign-in after switching accounts.

Where the token is stored

Not in the workspace folder. Chain tokens live in the macOS keychain, Windows Credential Manager or the Linux password store, separately for each environment. You can put the collection and the chain in git next to the app code: no tokens end up in the repository. On the arrows, in runs and in request history the token is hidden behind dots.

What you get

Checking the protected catalog is one click instead of four steps with copying. The authorization server sees one sign-in instead of dozens. And the token refresh your users go through every day is tested on staging before a bug turns up in it.

Build a chain for your own protected API in beta 0.9.6 and write to beta@microkoi.com about how your authorization works and whether the “Authorization” block handled it. We read every email.

Microkoi BenchAPI clientCollections, environments, history

Try it on your own project

The beta is free and needs no sign-up. Download it, pick your project folder and start the proxy — your first requests will show up within minutes.

Version 0.9.6 · macOS, Windows and Linux · no sign-up