Skip to main content
Most APIs require authentication. This tutorial demonstrates a common pattern: log in to get an access token, capture it, and pass it to all subsequent requests. You will use captures, dependencies, and the @auth directive.

Prerequisites

The Complete Test File

Create a file called auth-flow.http:
auth-flow.http

Step-by-Step Breakdown

1

Login and capture the token

The first request simulates a login and captures credentials from the response.
The >>>capture block extracts values from the response body. These become available as {{login.token}} and {{login.userId}} in later requests.
In a real API, you would capture from a path like body.access_token or body.data.token. This example uses httpbin.org which echoes back the posted JSON.
2

Use the token with @auth bearer

The @auth bearer directive automatically adds an Authorization: Bearer <token> header.
Key points:
  • # @depends login ensures this runs after the login request
  • # @auth bearer {{login.token}} injects the captured token
  • httpbin.org’s /bearer endpoint validates the Bearer token and returns authenticated: true
This is equivalent to manually writing the header:
But @auth bearer is cleaner and makes the intent explicit.
3

Chain multiple authenticated requests

Multiple requests can depend on the same login and reuse the token.
The startsWith operator checks that the Authorization header was sent correctly.
4

Dependent chain: login -> getProfile -> updateProfile

You can create multi-level dependency chains.
updateProfile depends on getProfile, which depends on login. hitspec resolves the full dependency graph automatically.
5

Basic authentication

hitspec supports multiple auth types. Here is Basic auth:
The @auth basic directive base64-encodes the credentials and sends them as Authorization: Basic <encoded>.
6

Run the auth tests

Run all auth tests:
Run only profile-related tests:
Expected output:

Supported Auth Types

hitspec supports eight authentication methods. Here are the most common:

Real-World Pattern

In practice, an auth flow test file typically looks like this:

Next Steps

Captures

Learn all capture sources: body, headers, status, and duration.

Dependencies

Understand how hitspec resolves the dependency graph.
Last modified on June 16, 2026