Get the FREE Ultimate OpenClaw Setup Guide →

api-response-mocker

Scanned
npx machina-cli add skill dkyazzentwatwa/chatgpt-skills/api-response-mocker --openclaw
Files (1)
SKILL.md
5.0 KB

API Response Mocker

Generate realistic mock API responses with fake data using Faker.

Features

  • Schema-Based Generation: Define response structure
  • Faker Integration: Realistic fake data
  • Nested Objects: Complex nested structures
  • Arrays: Generate lists of objects
  • Relationships: Reference other mock data
  • Multiple Formats: JSON, XML output

Quick Start

from api_mocker import APIMocker

mocker = APIMocker()

# Generate user response
user = mocker.generate({
    "id": "uuid",
    "name": "name",
    "email": "email",
    "created_at": "datetime"
})

# Generate list of users
users = mocker.generate_list({
    "id": "uuid",
    "name": "name",
    "email": "email"
}, count=10)

CLI Usage

# Generate from schema file
python api_mocker.py --schema user_schema.json --output user.json

# Generate list
python api_mocker.py --schema product.json --count 50 --output products.json

# Generate with seed (reproducible)
python api_mocker.py --schema order.json --seed 42 --output order.json

# Preview without saving
python api_mocker.py --schema customer.json --preview

Schema Format

Define fields using Faker provider names:

{
    "id": "uuid",
    "first_name": "first_name",
    "last_name": "last_name",
    "email": "email",
    "phone": "phone_number",
    "company": "company",
    "address": {
        "street": "street_address",
        "city": "city",
        "state": "state",
        "zip": "zipcode",
        "country": "country"
    },
    "created_at": "date_time_this_year",
    "is_active": "boolean"
}

Available Data Types

Personal

  • name, first_name, last_name
  • email, safe_email
  • phone_number
  • ssn

Address

  • address, street_address
  • city, state, state_abbr
  • zipcode, postcode
  • country, country_code
  • latitude, longitude

Internet

  • url, domain_name
  • ipv4, ipv6
  • user_name, password
  • uuid, uuid4
  • mac_address

Business

  • company, company_suffix
  • job, job_title
  • bs, catch_phrase

Financial

  • credit_card_number
  • iban, bban
  • currency_code
  • price (custom: returns float)

Date/Time

  • date, time
  • date_time, date_time_this_year
  • date_of_birth
  • iso8601

Text

  • text, sentence, paragraph
  • word, words
  • slug

Numeric

  • random_int, random_number
  • random_float (use {"type": "float", "min": 0, "max": 100})
  • boolean

Advanced Schemas

Arrays

{
    "id": "uuid",
    "name": "name",
    "tags": {
        "_array": true,
        "_count": 3,
        "_item": "word"
    },
    "orders": {
        "_array": true,
        "_count": 5,
        "_item": {
            "order_id": "uuid",
            "amount": "random_int",
            "date": "date"
        }
    }
}

Custom Values

{
    "id": "uuid",
    "status": {
        "_choice": ["pending", "active", "completed"]
    },
    "priority": {
        "_range": [1, 5]
    },
    "score": {
        "_float": {"min": 0.0, "max": 100.0, "decimals": 2}
    }
}

Nested Objects

{
    "user": {
        "id": "uuid",
        "profile": {
            "bio": "paragraph",
            "avatar_url": "image_url",
            "social": {
                "twitter": "user_name",
                "linkedin": "url"
            }
        }
    }
}

API Reference

APIMocker Class

class APIMocker:
    def __init__(self, locale: str = "en_US", seed: int = None)

    # Generation
    def generate(self, schema: dict) -> dict
    def generate_list(self, schema: dict, count: int = 10) -> list

    # File operations
    def from_schema_file(self, filepath: str) -> dict
    def save(self, data: any, filepath: str, format: str = "json")

    # Utilities
    def set_seed(self, seed: int)
    def get_faker(self) -> Faker

Example Schemas

User Response

{
    "id": "uuid",
    "username": "user_name",
    "email": "email",
    "profile": {
        "first_name": "first_name",
        "last_name": "last_name",
        "avatar": "image_url",
        "bio": "sentence"
    },
    "created_at": "iso8601",
    "last_login": "date_time_this_month"
}

E-commerce Product

{
    "sku": "uuid",
    "name": "catch_phrase",
    "description": "paragraph",
    "price": {"_float": {"min": 9.99, "max": 999.99}},
    "currency": "currency_code",
    "category": {"_choice": ["Electronics", "Clothing", "Home", "Sports"]},
    "in_stock": "boolean",
    "rating": {"_float": {"min": 1, "max": 5, "decimals": 1}},
    "reviews_count": {"_range": [0, 500]}
}

API Error Response

{
    "error": {
        "code": {"_choice": ["NOT_FOUND", "UNAUTHORIZED", "BAD_REQUEST"]},
        "message": "sentence",
        "request_id": "uuid",
        "timestamp": "iso8601"
    }
}

Dependencies

  • faker>=22.0.0

Source

git clone https://github.com/dkyazzentwatwa/chatgpt-skills/blob/main/api-response-mocker/SKILL.mdView on GitHub

Overview

api-response-mocker generates realistic mock API responses by defining a schema and filling it with Faker data. It supports nested objects, arrays, and relationships, and can output JSON or XML, making testing, prototyping, and frontend development faster.

How This Skill Works

It uses a schema where fields map to Faker providers (e.g., uuid, name, email, date_time). APIMocker traverses the schema to generate a single object or a list, supporting nested objects, arrays (via _array and _count), and relationships, and can render output as JSON or XML.

When to Use It

  • Frontend development: populate UI components with realistic user data
  • Prototype or mock backend endpoints without building real services
  • Frontend testing and integration tests needing structured, varied data
  • Documentation samples showing typical API responses
  • Seeded experiments for reproducible results

Quick Start

  1. Step 1: Import APIMocker from api_mocker
  2. Step 2: Create an instance: mocker = APIMocker()
  3. Step 3: Generate data with mocker.generate({...}) or mocker.generate_list({...}, count=10)

Best Practices

  • Start with a minimal schema and iteratively add fields
  • Use arrays with _count and _item to model lists
  • Leverage _choice, _range, and _float for varied data
  • Set a seed for reproducible runs during tests
  • Validate generated data against the intended API contract

Example Use Cases

  • User profile endpoint returning id, name, email, and created_at
  • Product catalog with nested category and price fields
  • Order list with order_id, amount, and date
  • Blog posts feed with comments array
  • Event payload featuring venue details and attendees

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers