Dynamic Templates

Generate realistic test data dynamically using dotMock's built-in template functions.

Basic Syntax

Use double curly braces to insert template expressions in your JSON responses:

{
  "id": "{{uuid}}",
  "name": "{{randomName}}",
  "email": "{{randomEmail}}",
  "age": {{randomInt(18, 65)}},
  "joinedAt": "{{date('-1y')}}"
}

Common Template Functions

Person Functions

{{randomName}}→ "John Smith"
{{randomEmail}}→ "[email protected]"
{{randomPhone}}→ "+1-555-123-4567"
{{randomPhone('UK')}}→ "+44-7-1234-5678"
{{randomCompany}}→ "Tech Solutions Inc"
{{randomAddress}}→ "123 Main St, New York, NY 10001"

Advanced Usage

Function Arguments

Many template functions accept arguments to customize the output:

{{randomInt(100, 999)}}Generates an integer between 100 and 999
{{lorem(5)}}Generates exactly 5 lorem ipsum words
{{randomString(16)}}Generates a random string of 16 characters
{{randomChoice('active', 'inactive', 'pending')}}Randomly selects one value from the provided options

Date Offsets

The date function supports relative time offsets:

+1d - Tomorrow
-1d - Yesterday
+1w - Next week
-1w - Last week
+1m - Next month
-1m - Last month
+1y - Next year
-1y - Last year

Complete Example

Here's a comprehensive example using various template functions:

{
  "users": [
    {
      "id": "{{uuid}}",
      "profile": {
        "name": "{{randomName}}",
        "email": "{{randomEmail}}",
        "phone": "{{randomPhone}}",
        "avatar": "https://avatars.githubusercontent.com/u/{{randomInt(1000, 9999)}}",
        "bio": "{{lorem(15)}}"
      },
      "address": "{{randomAddress}}",
      "company": "{{randomCompany}}",
      "subscription": {
        "plan": "{{randomChoice('free', 'pro', 'enterprise')}}",
        "status": "{{randomChoice('active', 'cancelled', 'expired')}}",
        "expiresAt": "{{date('+1y')}}"
      },
      "stats": {
        "loginCount": {{randomInt(1, 1000)}},
        "lastLogin": "{{date('-3d')}}",
        "isVerified": {{randomBool}},
        "rating": {{randomFloat(1, 5)}}
      },
      "createdAt": "{{date('-1y')}}",
      "updatedAt": "{{now}}"
    }
  ],
  "meta": {
    "total": {{randomInt(50, 500)}},
    "page": 1,
    "perPage": 10,
    "timestamp": "{{now}}"
  }
}

Fun Functions

We also include some fun functions for testing:

{{animal.dog}}→ Returns a random dog breed like "Golden Retriever"
{{animal.cat}}→ Returns a random cat breed like "Persian"

Quick Reference

Identifiers & Numbers

  • uuid
  • randomInt(min, max)
  • randomFloat(min, max)
  • number.int

Person & Business

  • randomName
  • randomEmail
  • randomPhone
  • randomCompany
  • randomAddress

Random & Utilities

  • randomBool
  • randomChoice(...)
  • randomString(length)
  • lorem(words)

Date & Time

  • now
  • now(format)
  • date(offset)
  • date(offset, format)

Animals

  • animal.dog
  • animal.cat