{
  "openapi": "3.1.0",
  "info": {
    "title": "anticipated.io API",
    "version": "1.0.0",
    "summary": "Schedule webhooks and Amazon SQS messages for any future time.",
    "description": "Create an event with a time and a destination; anticipated delivers it at that time, retries failures (three attempts in total, 30 s then 2 min apart), signs webhooks and logs every attempt. Every response uses the same envelope: `request`, `context`, `message` (empty on success) plus the payload.",
    "contact": {
      "email": "help@anticipated.io",
      "url": "https://anticipated.io"
    }
  },
  "servers": [
    {
      "url": "https://api.anticipated.io"
    }
  ],
  "security": [
    {
      "apiKey": []
    },
    {
      "bearer": []
    }
  ],
  "paths": {
    "/v1/event": {
      "post": {
        "operationId": "createEvent",
        "summary": "Schedule an event",
        "description": "With a `key`, the same request sent again returns the same event (no duplicate, counted once); the same key with a different request is a 409. For SQS events without saved account credentials, pass `x-aws-access-key` and `x-aws-secret-access-key`.",
        "parameters": [
          {
            "name": "x-aws-access-key",
            "in": "header",
            "schema": {
              "type": "string"
            },
            "description": "SQS only: AWS access key id for this event."
          },
          {
            "name": "x-aws-secret-access-key",
            "in": "header",
            "schema": {
              "type": "string"
            },
            "description": "SQS only: AWS secret access key for this event (stored encrypted with KMS)."
          },
          {
            "name": "x-aws-region",
            "in": "header",
            "schema": {
              "type": "string"
            },
            "description": "SQS only: overrides the region read from the queue URL."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateEvent"
              },
              "example": {
                "when": "3 days from now",
                "timezone": "America/Phoenix",
                "type": "json",
                "key": "user-482-trial",
                "details": {
                  "url": "https://myapp.com/hooks/trial-ending",
                  "method": "post",
                  "document": {
                    "userId": 482
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Event"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/event/{id}": {
      "parameters": [
        {
          "$ref": "#/components/parameters/EventId"
        }
      ],
      "get": {
        "operationId": "getEvent",
        "summary": "Read an event and its delivery attempts",
        "responses": {
          "200": {
            "$ref": "#/components/responses/EventWithAttempts"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        }
      },
      "patch": {
        "operationId": "rescheduleEvent",
        "summary": "Move an event that has not fired yet",
        "description": "Only events that have not been attempted (status queued, scheduled or parked) can move. The id, key, payload and destination stay the same.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "when"
                ],
                "properties": {
                  "when": {
                    "$ref": "#/components/schemas/When"
                  },
                  "timezone": {
                    "$ref": "#/components/schemas/Timezone"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Event"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          }
        }
      },
      "delete": {
        "operationId": "cancelEvent",
        "summary": "Cancel an event",
        "description": "Idempotent: cancelling a cancelled event returns it unchanged. Events already delivered, failed or mid-delivery can't be cancelled (409).",
        "responses": {
          "200": {
            "$ref": "#/components/responses/Event"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/event/{id}/replay": {
      "parameters": [
        {
          "$ref": "#/components/parameters/EventId"
        }
      ],
      "post": {
        "operationId": "replayEvent",
        "summary": "Deliver a delivered or failed event again, now",
        "description": "Gives the event up to three more attempts. Counts as one event toward the monthly allowance.",
        "responses": {
          "200": {
            "$ref": "#/components/responses/Event"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/events": {
      "get": {
        "operationId": "listEvents",
        "summary": "List events by fire time, or find one by your key",
        "parameters": [
          {
            "name": "key",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Return the event with this caller key (other filters are ignored)."
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Comma-separated statuses.",
            "example": "queued,scheduled,parked"
          },
          {
            "name": "from",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Earliest fire time, ISO 8601 or epoch milliseconds."
          },
          {
            "name": "to",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Latest fire time, ISO 8601 or epoch milliseconds."
          },
          {
            "name": "order",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "asc",
                "desc"
              ],
              "default": "desc"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 200,
              "default": 50
            },
            "description": "Items read per page. The status filter applies after reading, so a page can hold fewer; keep following nextCursor."
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "nextCursor from the previous page."
          }
        ],
        "responses": {
          "200": {
            "description": "A page of events.",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/Envelope"
                    },
                    {
                      "type": "object",
                      "properties": {
                        "events": {
                          "type": "array",
                          "items": {
                            "$ref": "#/components/schemas/Event"
                          }
                        },
                        "nextCursor": {
                          "type": [
                            "string",
                            "null"
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key",
        "description": "Your API key. Test keys act on the test environment."
      },
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "The same API key as a bearer token."
      }
    },
    "parameters": {
      "EventId": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        },
        "description": "Event id returned at creation."
      }
    },
    "responses": {
      "Event": {
        "description": "The event.",
        "content": {
          "application/json": {
            "schema": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Envelope"
                },
                {
                  "type": "object",
                  "properties": {
                    "now": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "event": {
                      "$ref": "#/components/schemas/Event"
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "EventWithAttempts": {
        "description": "The event with its delivery attempts.",
        "content": {
          "application/json": {
            "schema": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Envelope"
                },
                {
                  "type": "object",
                  "properties": {
                    "now": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "event": {
                      "allOf": [
                        {
                          "$ref": "#/components/schemas/Event"
                        },
                        {
                          "type": "object",
                          "properties": {
                            "attempts": {
                              "type": "array",
                              "items": {
                                "$ref": "#/components/schemas/Attempt"
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "Error": {
        "description": "Error; `message` says what went wrong.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Envelope"
            }
          }
        }
      }
    },
    "schemas": {
      "When": {
        "description": "ISO 8601 (with or without offset), epoch milliseconds, or plain words such as \"in 20 minutes\" or \"next Tuesday at 9am\". Must be in the future.",
        "oneOf": [
          {
            "type": "string"
          },
          {
            "type": "integer"
          }
        ]
      },
      "Timezone": {
        "type": "string",
        "description": "IANA zone used to read `when` when it has no offset. Default UTC.",
        "example": "America/Phoenix"
      },
      "CreateEvent": {
        "type": "object",
        "required": [
          "when",
          "type",
          "details"
        ],
        "properties": {
          "when": {
            "$ref": "#/components/schemas/When"
          },
          "timezone": {
            "$ref": "#/components/schemas/Timezone"
          },
          "type": {
            "type": "string",
            "enum": [
              "json",
              "xml",
              "sqs"
            ],
            "description": "json: webhook with the JSON envelope. xml: webhook with `document` sent as is (application/xml). sqs: message to an SQS queue."
          },
          "key": {
            "type": "string",
            "maxLength": 200,
            "description": "Your own id for the event, unique per environment. Makes creation idempotent and lets you find the event with GET /v1/events?key=."
          },
          "details": {
            "type": "object",
            "required": [
              "url"
            ],
            "properties": {
              "url": {
                "type": "string",
                "format": "uri",
                "description": "Public https URL, or an SQS queue URL for type sqs."
              },
              "method": {
                "type": "string",
                "enum": [
                  "get",
                  "post",
                  "put",
                  "delete"
                ],
                "default": "post"
              },
              "document": {
                "description": "Your payload: any JSON for json and sqs, a string for xml."
              },
              "headers": {
                "type": "object",
                "additionalProperties": {
                  "type": "string"
                },
                "description": "Extra headers sent with the webhook."
              }
            }
          }
        }
      },
      "Event": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "company": {
            "type": "string",
            "description": "Account id."
          },
          "type": {
            "type": "string",
            "enum": [
              "json",
              "xml",
              "sqs"
            ]
          },
          "when": {
            "type": "string",
            "format": "date-time",
            "description": "Exact fire time, UTC."
          },
          "created": {
            "type": "string",
            "format": "date-time"
          },
          "details": {
            "type": "object"
          },
          "processed": {
            "type": "boolean",
            "description": "Delivered or failed."
          },
          "deleted": {
            "type": "boolean",
            "description": "Cancelled."
          },
          "status": {
            "type": "string",
            "enum": [
              "queued",
              "scheduled",
              "parked",
              "delivering",
              "retrying",
              "delivered",
              "failed",
              "cancelled"
            ],
            "description": "queued, scheduled and parked are all waiting to fire (parked = more than 7 days out)."
          },
          "key": {
            "type": [
              "string",
              "null"
            ]
          },
          "timezone": {
            "type": "string"
          },
          "environment": {
            "type": "string",
            "enum": [
              "live",
              "test"
            ]
          },
          "attemptCount": {
            "type": "integer"
          },
          "lastStatusCode": {
            "type": [
              "integer",
              "null"
            ]
          },
          "lastMessage": {
            "type": [
              "string",
              "null"
            ]
          },
          "deliveredAt": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "updated": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Attempt": {
        "type": "object",
        "properties": {
          "attempt": {
            "type": "integer"
          },
          "at": {
            "type": "string",
            "format": "date-time"
          },
          "ok": {
            "type": "boolean",
            "description": "2xx response (webhooks) or message accepted (SQS)."
          },
          "statusCode": {
            "type": [
              "integer",
              "null"
            ]
          },
          "latencyMs": {
            "type": "integer"
          },
          "response": {
            "type": "string",
            "description": "Response body, truncated."
          },
          "error": {
            "type": "string"
          },
          "lateByMs": {
            "type": "integer",
            "description": "First attempt only: how long after the fire time it was sent."
          }
        }
      },
      "Envelope": {
        "type": "object",
        "properties": {
          "request": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              }
            }
          },
          "context": {
            "type": "object",
            "properties": {
              "user": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "company": {
                "type": [
                  "string",
                  "null"
                ]
              }
            }
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}
