{
  "openapi": "3.0.3",
  "info": {
    "title": "Tomorrow’s Tail Shelter API",
    "version": "1.0.0",
    "description": "Authenticate with `Authorization: Bearer <shelter API key>`. Rate limits: about 120 requests per minute per key (429 with a message when exceeded). Generated jobs share the shelter's daily illustration limit."
  },
  "servers": [
    {
      "url": "/api/v1"
    }
  ],
  "security": [
    {
      "shelterKey": []
    }
  ],
  "paths": {
    "/portraits": {
      "post": {
        "summary": "Submit a portrait or replay an existing request",
        "parameters": [
          {
            "in": "header",
            "name": "Idempotency-Key",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z0-9._:-]{1,128}$"
            }
          },
          {
            "in": "header",
            "name": "Prefer",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "wait=120"
              ]
            },
            "description": "Process the submitted job while this connection remains open."
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "default": {
            "description": "Request failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "202": {
            "description": "Accepted or in progress",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "429": {
            "description": "Daily generation limit reached",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Generation is unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Input"
              }
            }
          }
        }
      },
      "get": {
        "summary": "List shelter portrait summaries",
        "parameters": [
          {
            "in": "query",
            "name": "after_id",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Page"
                }
              }
            }
          },
          "default": {
            "description": "Request failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/portraits/{id}": {
      "get": {
        "summary": "Retrieve a labeled shelter photo and AI portrait",
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pair"
                }
              }
            }
          },
          "default": {
            "description": "Request failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/jobs/{id}": {
      "get": {
        "summary": "Read job status",
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "default": {
            "description": "Request failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/portrait-changes": {
      "get": {
        "summary": "Sync retirement, restoration and permanent deletion events",
        "parameters": [
          {
            "in": "query",
            "name": "after_id",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Up to 50 lifecycle events in ascending ID order",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "integer"
                          },
                          "portrait_id": {
                            "type": "integer"
                          },
                          "action": {
                            "type": "string",
                            "enum": [
                              "retired",
                              "restored",
                              "deleted"
                            ]
                          },
                          "at": {
                            "type": "integer"
                          }
                        }
                      }
                    },
                    "next_after_id": {
                      "type": "integer",
                      "nullable": true
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "shelterKey": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "Input": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "maxLength": 80
          },
          "note": {
            "type": "string",
            "maxLength": 500
          },
          "photo": {
            "type": "string",
            "description": "Full JPEG or PNG data URL, up to 5 MB decoded."
          },
          "card": {
            "$ref": "#/components/schemas/Card"
          }
        },
        "required": [
          "name",
          "photo"
        ]
      },
      "Job": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "queued",
              "processing",
              "completed",
              "failed"
            ]
          },
          "status_url": {
            "type": "string"
          },
          "portrait_id": {
            "type": "integer",
            "nullable": true
          },
          "portrait_url": {
            "type": "string",
            "nullable": true
          },
          "error": {
            "type": "string",
            "nullable": true
          }
        },
        "required": [
          "id",
          "status",
          "status_url",
          "portrait_id",
          "portrait_url",
          "error"
        ]
      },
      "Summary": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "note": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "name",
          "note"
        ]
      },
      "Pair": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "note": {
            "type": "string"
          },
          "photo": {
            "type": "string"
          },
          "illustration": {
            "type": "string"
          },
          "labels": {
            "type": "object",
            "properties": {
              "photo": {
                "type": "string"
              },
              "illustration": {
                "type": "string"
              }
            },
            "required": [
              "photo",
              "illustration"
            ]
          },
          "card": {
            "$ref": "#/components/schemas/Card"
          }
        },
        "required": [
          "id",
          "name",
          "note",
          "photo",
          "illustration",
          "labels"
        ]
      },
      "Page": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Summary"
            }
          },
          "next_after_id": {
            "type": "integer",
            "nullable": true
          }
        },
        "required": [
          "items",
          "next_after_id"
        ]
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        },
        "required": [
          "error"
        ]
      },
      "Card": {
        "type": "object",
        "description": "Optional saved studio print/download settings. The default template is ah when absent. Dates and estimates are shelter-provided.",
        "properties": {
          "template": {
            "type": "string",
            "enum": [
              "ah",
              "a",
              "b",
              "c",
              "d",
              "e",
              "f",
              "g",
              "h",
              "i",
              "j",
              "k",
              "l",
              "m",
              "n",
              "o",
              "p"
            ]
          },
          "careStart": {
            "type": "string",
            "format": "date"
          },
          "recheck": {
            "type": "string",
            "format": "date"
          },
          "estimate": {
            "type": "string",
            "maxLength": 100
          },
          "handoff": {
            "type": "string",
            "maxLength": 280
          },
          "publicNote": {
            "type": "string",
            "maxLength": 500
          },
          "animalId": {
            "type": "string",
            "maxLength": 64
          },
          "contact": {
            "type": "string",
            "maxLength": 240
          },
          "handoffDate": {
            "type": "string",
            "format": "date",
            "description": "Foster handoff date (template i)."
          },
          "updateNote": {
            "type": "string",
            "maxLength": 280,
            "description": "Care-team update (template n)."
          },
          "updateDate": {
            "type": "string",
            "format": "date",
            "description": "Date of the care-team update (template n)."
          },
          "hideContact": {
            "type": "boolean",
            "description": "Hide the contact line on the card."
          },
          "qrLink": {
            "type": "string",
            "maxLength": 200,
            "pattern": "^https?://",
            "description": "Profile link printed as a QR code (template o)."
          },
          "alsoId": {
            "type": "integer",
            "minimum": 1,
            "description": "Another active portrait in the same workspace to feature (template p)."
          }
        }
      }
    }
  }
}
