Mocko Helpers

Other than the handlebars-helpers, Mocko provides you with special helpers that can change your response directly, here are some of them:


Lets you set the response status dynamically or conditionally. Example GET /cats/{id}:

{{#is '1'}}
        "id": 1,
        "name": "George"
{{else is '2'}}
        "id": 2,
        "name": "Alice"
    {{! You can set the status conditionally from here with the 'setStatus' helper }}
    {{setStatus 404}}
        "error": "Not found error",
        "message": "Cat not found"


Lets you set response headers dynamically or conditionally. Example GET /purchase-tasks/{id}:

{{#gt (toInt 10}}
    {{! Simulating completed tasks with id greater than 10 }}
    {{setStatus 303}}
    {{setHeader 'Location' (append '/purchases/'}}
    {{! Simulating random progress tasks otherwise }}
        "id": {{}},
        "progress": {{random 0 100}}


Lets you proxy conditionally to the actual API behind mocko. You can choose the URL to proxy with the config PROXY_BASE-URI in the mocko-proxy module, either in the .env file or with an environment variable. If you're using the Mocko CLI, you can use the flag --url or -u instead.

In the example below we're mocking empty posts for user with id 1:

  • GET /posts?userId=1 will be mocked
  • GET /posts?userId=2 (and other users) will be proxied to the real API

Example GET /posts

{{! If the query "userId" is 1, return an empty array, otherwise proxy to the real API }}
{{#is request.query.userId 1}}

Alternatively, you can override the proxy URI for a specific mock by passing it as a parameter to proxy helper:

{{#is '1'}}
    {{proxy 'http://localhost:8082'}}
    {{proxy 'http://localhost:8081'}}


You can use the append helper to concatenate its parameters as a string:

{{append 'users:' ':name'}}


Generates an UUID v4. No parameters required.


Returns a substring:

{{substring 'Lorem ipsum' 0 4}}
{{! This will produce 'Lore' }}

Same usage as JavaScript's substring.

setFlag, getFlag, delFlag, hasFlag

Check their documentation under persistence.

get, set

Check their documentation under variables.