{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://apis.dev/definition.schema.json",
  "title": "definition.ts",
  "description": "The file you write: five blocks, as data, inside define(). A Noun whose fields name a Source in `from` gets `get` by its `#` key and `search` by a field; a Noun your Customers write, or an Integration holds, gets create, get, list, update and delete. A move enters its past-form state and writes that Event: `open` enters `opened` and writes `Deal.opened`; `win` enters `won`; `lose` enters `lost`. Every Verb you write, a move, a Function or a machine event, has a price, a plan or an outcome, or the publish is refused; a generated Verb serves when one covers it. A plan covers the Verbs it leaves unpriced, and an outcome every Verb of its Thing. `Noun.verb` becomes `verbNoun` over MCP and in the SDK, plural for a list: `Deal.open` is `openDeal`, `Company.search` is `searchCompanies`. A Trigger names its Verb in full: `'Deal.won': 'Deal.kickoff'`. A Verb that an `on` line starts and that names no `role` is internal: it serves no path, tool or command, and a direct call answers 404 `no_route`; while it runs, the Thing is in its activity state, `drafting`.",
  "type": "object",
  "properties": {
    "name": {
      "description": "The CLI command, the npm and PyPI package, the SDK class and the MCP server name; deploy asks for it when it is missing.",
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]*$"
    },
    "domain": {
      "type": "string",
      "pattern": "^[a-z0-9.-]+\\.[a-z]+$"
    },
    "sources": {
      "type": "object",
      "propertyNames": {
        "type": "string"
      },
      "additionalProperties": {
        "$ref": "#/$defs/Source"
      }
    },
    "on": {
      "$ref": "#/$defs/Trigger"
    },
    "offer": {
      "$ref": "#/$defs/Offer"
    }
  },
  "required": [
    "offer"
  ],
  "additionalProperties": {
    "$ref": "#/$defs/Noun"
  },
  "$defs": {
    "Source": {
      "type": "object",
      "properties": {
        "origin": {
          "type": "string",
          "pattern": "^https:\\/\\/"
        },
        "api": {
          "description": "A Source that is another API product, read through its own Verbs.",
          "type": "string",
          "pattern": "^[a-z0-9.-]+\\.[a-z]+$"
        },
        "key": {
          "$ref": "#/$defs/Secret"
        },
        "resources": {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {}
        },
        "schedule": {
          "type": "string",
          "pattern": "^every (day|weekday|(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(, (Mon|Tue|Wed|Thu|Fri|Sat|Sun))*) \\d{2}:\\d{2}$"
        },
        "live": {
          "type": "boolean"
        },
        "system": {
          "type": "string"
        },
        "scope": {
          "type": "string",
          "enum": [
            "you",
            "customer"
          ]
        },
        "license": {
          "type": "string"
        },
        "cost": {
          "description": "What each of the Source's calls costs you: deploy prints your margin per Verb, and a price below it is refused.",
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "type": "number",
            "minimum": 0
          }
        }
      },
      "additionalProperties": false,
      "description": "The Sources you draw on, each by the name `from` uses, at its origin."
    },
    "Secret": {
      "type": "object",
      "properties": {
        "$call": {
          "type": "string",
          "const": "secret"
        },
        "args": {
          "type": "array",
          "prefixItems": [
            {
              "type": "string",
              "pattern": "^[A-Z][A-Z0-9_]*$"
            }
          ],
          "items": false,
          "minItems": 1,
          "maxItems": 1
        }
      },
      "required": [
        "$call",
        "args"
      ],
      "additionalProperties": false,
      "description": "A Source's credential, set with `npx apis.dev secrets set FIRMS_KEY`: never in the file, never shown to a Customer."
    },
    "Trigger": {
      "type": "object",
      "propertyNames": {
        "anyOf": [
          {
            "type": "string",
            "pattern": "^[A-Z][A-Za-z]*\\.[a-z][A-Za-z]*$"
          },
          {
            "type": "string",
            "pattern": "^every (day|weekday|(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(, (Mon|Tue|Wed|Thu|Fri|Sat|Sun))*) \\d{2}:\\d{2}$"
          }
        ]
      },
      "additionalProperties": {
        "type": "string",
        "pattern": "^[A-Z][A-Za-z]*\\.[a-z][A-Za-z]*$"
      },
      "description": "A Trigger: when the Event happens, the Verb it names starts. Nothing else in the file starts work."
    },
    "Offer": {
      "type": "object",
      "properties": {
        "free": {
          "description": "Calls each month that cost nothing, on any plan or none.",
          "type": "object",
          "propertyNames": {
            "type": "string",
            "pattern": "^[A-Z][A-Za-z]*\\.[a-z][A-Za-z]*$"
          },
          "additionalProperties": {
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          }
        },
        "credits": {
          "description": "The credit packs Checkout sells; `{ pay, get }` is a pack with a bonus.",
          "minItems": 1,
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "number",
                "exclusiveMinimum": 0
              },
              {
                "type": "object",
                "properties": {
                  "pay": {
                    "type": "number",
                    "exclusiveMinimum": 0
                  },
                  "get": {
                    "type": "number",
                    "exclusiveMinimum": 0
                  }
                },
                "required": [
                  "pay",
                  "get"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "keyPrefix": {
          "description": "The prefix of every Key: `cf_test_…` and `cf_live_…`, then 30 characters and a checksum; with none, the file's name.",
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*$"
        }
      },
      "additionalProperties": {
        "anyOf": [
          {
            "$ref": "#/$defs/Plan"
          },
          {
            "type": "object",
            "properties": {
              "contact": {
                "type": "boolean",
                "const": true
              },
              "seats": {
                "type": "integer",
                "minimum": 1,
                "maximum": 9007199254740991
              }
            },
            "required": [
              "contact"
            ],
            "additionalProperties": false
          },
          {
            "type": "number",
            "minimum": 0
          },
          {
            "$ref": "#/$defs/Price"
          }
        ]
      },
      "description": "What each of the Source's calls costs you: deploy prints your margin per Verb, and a price below it is refused."
    },
    "Plan": {
      "type": "object",
      "properties": {
        "month": {
          "type": "number",
          "minimum": 0
        },
        "seats": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "seat": {
          "type": "number",
          "minimum": 0
        },
        "year": {
          "type": "number",
          "minimum": 0
        },
        "trial": {
          "type": "string",
          "pattern": "^\\d+(s|m|h|d|w)$"
        },
        "includes": {
          "type": "object",
          "propertyNames": {
            "type": "string",
            "pattern": "^[A-Z][A-Za-z]*\\.[a-z][A-Za-z]*$"
          },
          "additionalProperties": {
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          }
        },
        "keep": {
          "type": "boolean"
        }
      },
      "required": [
        "month"
      ],
      "additionalProperties": false,
      "description": "A capitalised key is a plan: a price a month for its seats, and `seat` for each seat past them; `year`, `trial` and `keep` are its other keys."
    },
    "Price": {
      "anyOf": [
        {
          "type": "number",
          "minimum": 0
        },
        {
          "type": "object",
          "properties": {
            "month": {
              "type": "number",
              "minimum": 0
            }
          },
          "required": [
            "month"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "per1000Tokens": {
              "type": "number",
              "minimum": 0
            },
            "perMinute": {
              "type": "number",
              "minimum": 0
            }
          },
          "required": [
            "per1000Tokens",
            "perMinute"
          ],
          "additionalProperties": false,
          "description": "A price by consumption: per 1,000 tokens and per minute."
        },
        {
          "type": "object",
          "properties": {
            "price": {
              "type": "number",
              "minimum": 0
            },
            "when": {
              "type": "string"
            },
            "accept": {
              "type": "string",
              "enum": [
                "customer",
                "you"
              ]
            },
            "acceptWithin": {
              "type": "string",
              "pattern": "^\\d+(s|m|h|d|w)$"
            },
            "remedy": {
              "type": "string",
              "enum": [
                "redo",
                "refund",
                "credit"
              ]
            }
          },
          "required": [
            "price",
            "when",
            "accept",
            "remedy"
          ],
          "additionalProperties": false,
          "description": "A price by outcome: charged once, when the Thing enters `when`; a rejected one gets its remedy: `redo`, `refund` or `credit`."
        }
      ]
    },
    "Noun": {
      "type": "object",
      "propertyNames": {
        "type": "string",
        "pattern": "^[a-z][A-Za-z]*$"
      },
      "additionalProperties": {
        "anyOf": [
          {
            "type": "string",
            "pattern": "^(string|number|boolean|date|url|decimal\\(\\d+,\\d+\\))[?!#]*$",
            "description": "A field's type, as a lower-case word."
          },
          {
            "type": "string",
            "pattern": "^(->|<-)[A-Z][A-Za-z]*\\.[a-z][A-Za-z]*$",
            "description": "A Relationship: `->` points at another Noun and `<-` reads one back; after the dot is its other side."
          },
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "pattern": "^(string|number|boolean|date|url|decimal\\(\\d+,\\d+\\))[?!#]*$",
                "description": "A field's type, as a lower-case word."
              },
              "from": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "$ref": "#/$defs/Function"
                  },
                  {
                    "minItems": 1,
                    "type": "array",
                    "items": {
                      "anyOf": [
                        {
                          "type": "string"
                        },
                        {
                          "$ref": "#/$defs/Function"
                        }
                      ]
                    }
                  }
                ]
              },
              "map": {
                "type": "string"
              },
              "example": {
                "description": "A field as an object: its type, and `example:`, the field's example in the docs, OpenAPI, the SDK and Try it.",
                "type": [
                  "string",
                  "number",
                  "boolean"
                ]
              }
            },
            "required": [
              "type"
            ],
            "additionalProperties": false,
            "description": "A field a Source writes: its type, the Source, and the Source's own field."
          },
          {
            "$ref": "#/$defs/Function"
          },
          {
            "$ref": "#/$defs/Workflow"
          },
          {
            "$ref": "#/$defs/Machine"
          },
          {
            "type": "object",
            "propertyNames": {
              "type": "string",
              "pattern": "^[a-z][A-Za-z]*$"
            },
            "additionalProperties": {
              "anyOf": [
                {
                  "anyOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  ]
                },
                {
                  "type": "object",
                  "properties": {
                    "in": {
                      "anyOf": [
                        {
                          "type": "string"
                        },
                        {
                          "type": "array",
                          "items": {
                            "type": "string"
                          }
                        }
                      ]
                    },
                    "role": {
                      "type": "string"
                    },
                    "run": {
                      "anyOf": [
                        {
                          "$ref": "#/$defs/Function"
                        },
                        {
                          "$ref": "#/$defs/Workflow"
                        }
                      ]
                    }
                  },
                  "required": [
                    "in"
                  ],
                  "additionalProperties": false
                }
              ]
            },
            "description": "Lifecycle moves: each key is a Verb, and its value the state it moves from; `[]` is no state, so `open` creates the Thing."
          }
        ]
      },
      "description": "A capitalised key is a Noun: the shape you promise, with its fields and its Verbs."
    },
    "Function": {
      "type": "object",
      "properties": {
        "$call": {
          "type": "string",
          "enum": [
            "code",
            "generative",
            "agentic",
            "human"
          ]
        },
        "args": {
          "anyOf": [
            {
              "type": "array",
              "prefixItems": [
                {
                  "type": "string"
                }
              ],
              "items": false,
              "minItems": 1,
              "maxItems": 1
            },
            {
              "type": "array",
              "prefixItems": [
                {
                  "type": "string"
                },
                {
                  "type": "object",
                  "properties": {
                    "returns": {
                      "description": "A Function's options: `returns` types its output everywhere, `input` its input on a Noun with no records, `tools` the Sources an agent may use, `role` who does a human one.",
                      "type": "object",
                      "propertyNames": {
                        "type": "string",
                        "pattern": "^[a-z][A-Za-z]*$"
                      },
                      "additionalProperties": {
                        "type": "string",
                        "pattern": "^(string|number|boolean|date|url|decimal\\(\\d+,\\d+\\))[?!#]*$",
                        "description": "A field's type, as a lower-case word."
                      }
                    },
                    "input": {
                      "type": "object",
                      "propertyNames": {
                        "type": "string",
                        "pattern": "^[a-z][A-Za-z]*$"
                      },
                      "additionalProperties": {
                        "type": "string",
                        "pattern": "^(string|number|boolean|date|url|decimal\\(\\d+,\\d+\\))[?!#]*$",
                        "description": "A field's type, as a lower-case word."
                      }
                    },
                    "tools": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "role": {
                      "type": "string"
                    }
                  },
                  "additionalProperties": false
                }
              ],
              "items": false,
              "minItems": 2,
              "maxItems": 2
            }
          ]
        }
      },
      "required": [
        "$call"
      ],
      "additionalProperties": false,
      "description": "A Function: `code`, `generative`, `agentic` or `human`. A key whose value is one is a Verb; a Verb with no comment gets one written from its Noun and kind."
    },
    "Workflow": {
      "type": "object",
      "properties": {
        "$call": {
          "type": "string",
          "const": "workflow"
        }
      },
      "required": [
        "$call"
      ],
      "additionalProperties": false,
      "description": "A Workflow as durable code: a key whose value is one is a Verb, the work's body; `$` gives each step."
    },
    "Machine": {
      "type": "object",
      "properties": {
        "$call": {
          "type": "string",
          "const": "machine"
        },
        "args": {
          "type": "array",
          "prefixItems": [
            {
              "type": "object",
              "properties": {
                "initial": {
                  "type": "string"
                },
                "states": {
                  "type": "object",
                  "propertyNames": {
                    "type": "string"
                  },
                  "additionalProperties": {
                    "type": "object",
                    "properties": {
                      "entry": {
                        "$ref": "#/$defs/Function"
                      },
                      "on": {
                        "type": "object",
                        "propertyNames": {
                          "type": "string",
                          "pattern": "^[a-z][A-Za-z]*$"
                        },
                        "additionalProperties": {
                          "anyOf": [
                            {
                              "type": "string"
                            },
                            {
                              "type": "object",
                              "properties": {
                                "target": {
                                  "type": "string"
                                },
                                "role": {
                                  "type": "string"
                                },
                                "guard": {
                                  "type": "string"
                                }
                              },
                              "required": [
                                "target"
                              ],
                              "additionalProperties": false
                            }
                          ]
                        }
                      },
                      "after": {
                        "type": "object",
                        "propertyNames": {
                          "type": "string",
                          "pattern": "^\\d+(s|m|h|d|w)$"
                        },
                        "additionalProperties": {
                          "type": "string"
                        }
                      },
                      "type": {
                        "type": "string",
                        "const": "final"
                      }
                    },
                    "additionalProperties": false
                  }
                }
              },
              "required": [
                "initial",
                "states"
              ],
              "additionalProperties": false
            }
          ],
          "items": false,
          "minItems": 1,
          "maxItems": 1
        }
      },
      "required": [
        "$call",
        "args"
      ],
      "additionalProperties": false,
      "description": "A Workflow as a state machine: each state's `entry`, its `on` events and its `after` timers."
    }
  },
  "propertyNames": {
    "anyOf": [
      {
        "enum": [
          "name",
          "domain",
          "sources",
          "on",
          "offer"
        ]
      },
      {
        "pattern": "^[A-Z][A-Za-z]*$"
      }
    ]
  }
}
