Production API Server

FakeDataDSL includes a production-ready REST API server for data generation. Use it as the core of your data generation platform.

Quick Start

# Start server with default settings
fake_data_dsl server schemas/ --port 3000

# Start with authentication
fake_data_dsl server schemas/ --port 3000 --api-key secret123

# Production mode with rate limiting
fake_data_dsl server schemas/ --port 3000 --env production --rate-limit 100

CLI Options

Option Description Default
-d, --dir DIR Schema directory Required
-p, --port PORT Server port 3000
-h, --host HOST Bind address 0.0.0.0
-e, --env ENV Environment development
--no-cors Disable CORS CORS enabled
--rate-limit N Requests/minute 0 (unlimited)
--api-key KEY Require API key None

API Endpoints

List Schemas

GET /api/schemas

Response:

{
  "schemas": [
    { "name": "User", "fields": 5, "endpoint": "/api/user" },
    { "name": "Order", "fields": 8, "endpoint": "/api/order" }
  ]
}

Generate Single Record

GET /api/:schema?seed=42&mode=random

Query Parameters:

  • seed - Random seed for deterministic output
  • mode - Generation mode: random, edge, invalid, hostile, mixed

Response:

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "name": "John Doe",
  "email": "john@example.com"
}

Generate Batch

GET /api/:schema/batch?count=100&seed=42

Response:

{
  "data": [...],
  "meta": {
    "count": 100,
    "schema": "User",
    "seed": 42,
    "mode": "random"
  }
}

Stream Records

GET /api/:schema/stream?count=1000

Returns NDJSON (newline-delimited JSON) for streaming:

{"id":"...","name":"John"}
{"id":"...","name":"Jane"}

Generate with Overrides

POST /api/:schema
Content-Type: application/json

{
  "count": 10,
  "seed": 42,
  "mode": "random",
  "overrides": {
    "role": "admin",
    "active": true
  }
}

Get Schema Info

GET /api/:schema/schema

Export OpenAPI/Protobuf

GET /api/:schema/openapi
GET /api/:schema/protobuf

Ruby API

require 'fake_data_dsl'

# Start server
FakeDataDSL::APIServer.start(
  schema_dir: "schemas/",
  port: 3000,
  host: "0.0.0.0",
  environment: :production,
  cors: true,
  rate_limit: 100,
  auth: { type: :api_key, keys: ["secret123"] },
  enable_metrics: true,
  enable_caching: true,
  cache_ttl: 60
)

# Start in background
thread = FakeDataDSL::APIServer.new(schema_dir: "schemas/").start_async

Rack Integration

Deploy with Puma, Unicorn, or any Rack-compatible server:

# config.ru
require 'fake_data_dsl'

run FakeDataDSL::APIServer.rack_app(
  schema_dir: "schemas/",
  auth: { type: :api_key, keys: [ENV['API_KEY']] }
)

Then run:

puma config.ru -p 3000 -t 4:16

Authentication

API Key

FakeDataDSL::APIServer.start(
  schema_dir: "schemas/",
  auth: { type: :api_key, keys: ["key1", "key2"] }
)

Clients must include:

X-API-Key: key1

Bearer Token

FakeDataDSL::APIServer.start(
  schema_dir: "schemas/",
  auth: { type: :bearer, tokens: ["token1", "token2"] }
)

Clients must include:

Authorization: Bearer token1

Rate Limiting

FakeDataDSL::APIServer.start(
  schema_dir: "schemas/",
  rate_limit: 100  # 100 requests per minute per IP
)

When rate limited, returns:

{
  "error": "Rate limit exceeded",
  "retry_after": 45
}

Metrics

Enable metrics with enable_metrics: true:

GET /metrics

Response:

{
  "total_requests": 1250,
  "endpoints": [
    {
      "endpoint": "GET /api/user",
      "requests": 500,
      "errors": 2,
      "avg_duration_ms": 15.5,
      "p99_duration_ms": 45.2
    }
  ]
}

Configuration

All configuration options:

Option Type Default Description
port Integer 3000 Server port
host String "0.0.0.0" Bind address
environment Symbol :development :development or :production
cors Boolean true Enable CORS
rate_limit Integer 0 Requests/minute (0=unlimited)
max_batch_size Integer 10,000 Max records per batch
max_stream_size Integer 1,000,000 Max streaming records
request_timeout Integer 30 Request timeout (seconds)
enable_metrics Boolean true Enable /metrics endpoint
enable_caching Boolean false Enable response caching
cache_ttl Integer 60 Cache TTL (seconds)
auth Hash nil Authentication config

Docker Deployment

FROM ruby:3.2

WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY schemas/ ./schemas/

EXPOSE 3000

CMD ["fake_data_dsl", "server", "schemas/", "--port", "3000", "--env", "production"]
docker build -t fake-data-api .
docker run -p 3000:3000 fake-data-api