Skip to main content
Variables allow you to define reusable values and reference them throughout your HTTPSpec files. This is useful for authentication tokens, API keys, base URLs, and other values you might want to change or reuse.
Variable substitution is defined in the HTTPSpec specification but not yet implemented in the current version of the tool. This page documents the planned syntax based on the specification.

Variable Definition

Define variables using the @ prefix followed by the variable name, an equals sign, and the value:
@variable_name = value

Examples

Variable Definitions
@token = abc123
@base_url = https://api.example.com
@user_id = 12345
Variable definitions should appear before the requests that use them.

Variable Reference

Reference variables in your requests using double curly braces:
{{variable_name}}

In URLs

URL Variables
@base_url = https://api.example.com
@user_id = 123

GET {{base_url}}/users/{{user_id}}

In Headers

Header Variables
@token = abc123

GET https://api.example.com/users/123
Authorization: Bearer {{token}}
Accept: application/json

In Request Bodies

Body Variables
@username = alice
@email = alice@example.com

POST https://api.example.com/users
Content-Type: application/json

{
  "name": "{{username}}",
  "email": "{{email}}"
}

Complete Example from Specification

Here’s the full example from the HTTPSpec specification document:
Complete Variable Example
@token = abc123

### Get user by ID
GET https://api.example.com/users/123
Authorization: Bearer {{token}}
Accept: application/json

//# status == 200
//# header["Content-Type"] == "application/json"
//# body.id == 123
//# body.email contains "@example.com"

### Create new user
POST https://api.example.com/users
Content-Type: application/json

{
    "name": "Alice"
}

//# status == 201
//# body.name == "Alice"

Use Cases

Define your auth token once and use it across multiple requests:
@auth_token = your-secret-token

GET https://api.example.com/users
Authorization: Bearer {{auth_token}}

GET https://api.example.com/posts
Authorization: Bearer {{auth_token}}
Switch between development, staging, and production:
@api_url = https://staging-api.example.com

GET {{api_url}}/users
GET {{api_url}}/posts
Reuse test data values:
@test_user_id = 123
@test_email = test@example.com

GET https://api.example.com/users/{{test_user_id}}

//# body contains "{{test_email}}"

Implementation Status

Variable substitution is currently not implemented. Variables defined with @ and referenced with {{}} will not be replaced in the current version of HTTPSpec.

Planned Features

According to the HTTPSpec specification, future versions may support:
  • Response variable extraction - Capture values from responses and use them in subsequent requests
  • Environment variables - Reference system environment variables in test files
  • Computed variables - Variables with dynamic values based on expressions

Current Workarounds

Until variable substitution is implemented, you can:
  1. Use environment variables in your shell and process files with envsubst before running HTTPSpec
  2. Use templating tools to generate .http files from templates
  3. Hard-code values directly in your test files
envsubst Example
# Define environment variables
export API_TOKEN=abc123
export BASE_URL=https://api.example.com

# Use envsubst to replace variables
envsubst < template.http > test.http
httpspec test.http

Syntax Reference

SyntaxPurposeStatus
@var = valueDefine a variablePlanned
{{var}}Reference a variablePlanned
@var = ${ENV_VAR}Environment variableFuture
Response extractionCapture from responsesFuture

Request Format

Learn the complete HTTP request syntax

Assertions

How to validate responses with assertions

Build docs developers (and LLMs) love