You're reading Mocko v1 (legacy) documentation. Mocko v2 is the current version. View the v2 docs.

Variables

Variables let you store a value within a single request and reuse it later in the same template. Unlike Flags, variables are not persisted; they exist only for the duration of the current request.

set and get

Use set to store a value under a name, and get to retrieve it:

{{set 'id' (uuid)}}
{
"id": "{{get 'id'}}",
"link": "/users/{{get 'id'}}"
}

This computes the UUID once and reuses the same value in multiple places, so both id and link always reference the same generated ID.

Common use case with flags

Variables are most useful when combined with flag helpers. Generate a UUID for a new resource, persist the resource data in flags, and return the ID in the response:

mock "POST /items" {
headers {
Content-Type = "application/json"
}
body = <<EOF
{{set 'id' (uuid)}}
{{setFlag (append 'items:' (get 'id') ':name') request.body.name}}
{
"id": "{{get 'id'}}"
}
EOF
}