{
  "openapi": "3.1.0",
  "info": {
    "title": "BokaTid Public Booking API",
    "version": "1.0.0",
    "description": "A narrow API for finding businesses and services, searching real availability, preparing a booking, and confirming it after explicit consent. Internal tenant identifiers, calendar tokens, database names, paths, and internal numeric IDs are never part of the public contract."
  },
  "servers": [
    {
      "url": "https://api.bokatid.nu"
    }
  ],
  "security": [
    {
      "bearerApiKey": []
    }
  ],
  "tags": [
    {
      "name": "Discovery"
    },
    {
      "name": "Availability"
    },
    {
      "name": "Booking"
    }
  ],
  "paths": {
    "/api/public/v1/businesses": {
      "get": {
        "operationId": "searchBusinesses",
        "summary": "Search businesses",
        "description": "Returns only active businesses that have opted in to external AI search.",
        "tags": ["Discovery"],
        "parameters": [
          {
            "name": "query",
            "in": "query",
            "schema": { "type": "string", "maxLength": 200 },
            "example": "massage"
          },
          {
            "name": "city",
            "in": "query",
            "schema": { "type": "string", "maxLength": 100 },
            "example": "Stockholm"
          },
          {
            "$ref": "#/components/parameters/Limit"
          }
        ],
        "responses": {
          "200": {
            "description": "Matching businesses.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BusinessSearchResponse" },
                "example": {
                  "items": [
                    {
                      "businessId": "bb833d31-5524-4592-9a33-bc0825815bac",
                      "name": "Exempelkliniken",
                      "description": "Massage och friskvård.",
                      "city": "Stockholm",
                      "countryCode": "SE"
                    }
                  ],
                  "count": 1
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/InternalError" }
        }
      }
    },
    "/api/public/v1/businesses/{businessId}/services": {
      "get": {
        "operationId": "searchServices",
        "summary": "Search services",
        "description": "Returns active services for one public business GUID.",
        "tags": ["Discovery"],
        "parameters": [
          { "$ref": "#/components/parameters/BusinessId" },
          {
            "name": "query",
            "in": "query",
            "schema": { "type": "string", "maxLength": 200 },
            "example": "klassisk"
          },
          {
            "name": "category",
            "in": "query",
            "schema": { "type": "string", "maxLength": 100 },
            "example": "Massage"
          },
          { "$ref": "#/components/parameters/Limit" }
        ],
        "responses": {
          "200": {
            "description": "Matching services.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ServiceSearchResponse" },
                "example": {
                  "items": [
                    {
                      "serviceId": "f44109b4-a6ad-44bb-864c-6c317772a745",
                      "businessId": "bb833d31-5524-4592-9a33-bc0825815bac",
                      "name": "Klassisk massage 60 min",
                      "description": "Individuellt anpassad behandling.",
                      "category": "Massage",
                      "durationMinutes": 60,
                      "price": 795,
                      "currency": "SEK"
                    }
                  ],
                  "count": 1
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/InternalError" }
        }
      }
    },
    "/api/public/v1/availability/search": {
      "post": {
        "operationId": "searchAvailability",
        "summary": "Search real availability",
        "description": "Searches live availability using the same booking rules as BokaTid's calendar. slotRef is encrypted, bound to the API client and exact slot, and expires after five minutes.",
        "tags": ["Availability"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/AvailabilitySearchRequest" },
              "example": {
                "businessId": "bb833d31-5524-4592-9a33-bc0825815bac",
                "serviceId": "f44109b4-a6ad-44bb-864c-6c317772a745",
                "dateFrom": "2026-08-03",
                "dateTo": "2026-08-07",
                "timeAfter": "09:00",
                "timeBefore": "17:00",
                "limit": 20
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Live bookable slots.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AvailabilitySearchResponse" },
                "example": {
                  "items": [
                    {
                      "slotRef": "CfDJ8K...opaque-short-lived-reference",
                      "businessId": "bb833d31-5524-4592-9a33-bc0825815bac",
                      "serviceId": "f44109b4-a6ad-44bb-864c-6c317772a745",
                      "start": "2026-08-03T10:00:00+02:00",
                      "end": "2026-08-03T11:00:00+02:00",
                      "staffName": "Anna",
                      "resourceName": "Behandlingsrum 1",
                      "price": 795,
                      "currency": "SEK",
                      "expiresAt": "2026-08-03T07:05:00Z"
                    }
                  ],
                  "count": 1
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/TemporarilyUnavailable" }
        }
      }
    },
    "/api/public/v1/bookings/prepare": {
      "post": {
        "operationId": "prepareBooking",
        "summary": "Prepare a booking",
        "description": "Revalidates availability and price, then creates a ten-minute booking intent. This operation never creates a booking and always requires a separate explicit confirmation.",
        "tags": ["Booking"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/PrepareBookingRequest" },
              "example": {
                "slotRef": "CfDJ8K...opaque-short-lived-reference",
                "customer": {
                  "firstName": "Erika",
                  "lastName": "Andersson",
                  "email": "erika@example.com",
                  "phone": "+46701234567"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Prepared booking intent. Show the summary to the user and ask for explicit confirmation.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PrepareBookingResponse" },
                "example": {
                  "intentId": "18e83774-65ef-4944-8c8c-b2296ca6605d",
                  "confirmationRef": "CfDJ8O...opaque-short-lived-reference",
                  "requiresExplicitConfirmation": true,
                  "expiresAt": "2026-08-03T07:10:00Z",
                  "summary": {
                    "businessId": "bb833d31-5524-4592-9a33-bc0825815bac",
                    "serviceId": "f44109b4-a6ad-44bb-864c-6c317772a745",
                    "businessName": "Exempelkliniken",
                    "serviceName": "Klassisk massage 60 min",
                    "start": "2026-08-03T10:00:00+02:00",
                    "end": "2026-08-03T11:00:00+02:00",
                    "price": 795,
                    "currency": "SEK"
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "410": { "$ref": "#/components/responses/Expired" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/InternalError" }
        }
      }
    },
    "/api/public/v1/bookings/confirm": {
      "post": {
        "operationId": "confirmBooking",
        "summary": "Confirm a prepared booking",
        "description": "Creates a confirmed booking only when confirm is true. The selected slot and price are revalidated immediately before the transactional booking write. Repeating the same request with the same Idempotency-Key returns the stored response without creating another booking.",
        "tags": ["Booking"],
        "parameters": [
          { "$ref": "#/components/parameters/IdempotencyKey" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ConfirmBookingRequest" },
              "example": {
                "confirmationRef": "CfDJ8O...opaque-short-lived-reference",
                "confirm": true
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Confirmed booking. bookingId is a public GUID, not an internal booking ID.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ConfirmBookingResponse" },
                "example": {
                  "bookingId": "394858cc-b8f6-44eb-a59b-59a75d52e5c6",
                  "status": "confirmed",
                  "confirmedAt": "2026-08-03T07:01:12Z",
                  "summary": {
                    "businessId": "bb833d31-5524-4592-9a33-bc0825815bac",
                    "serviceId": "f44109b4-a6ad-44bb-864c-6c317772a745",
                    "businessName": "Exempelkliniken",
                    "serviceName": "Klassisk massage 60 min",
                    "start": "2026-08-03T10:00:00+02:00",
                    "end": "2026-08-03T11:00:00+02:00",
                    "price": 795,
                    "currency": "SEK"
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "410": { "$ref": "#/components/responses/Expired" },
          "422": { "$ref": "#/components/responses/BookingRejected" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/TemporarilyUnavailable" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerApiKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "External API key issued by a BokaTid superadministrator. Keys are shown once and stored by BokaTid only as SHA-256 hashes."
      }
    },
    "parameters": {
      "BusinessId": {
        "name": "businessId",
        "in": "path",
        "required": true,
        "schema": { "type": "string", "format": "uuid" }
      },
      "Limit": {
        "name": "limit",
        "in": "query",
        "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 }
      },
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": true,
        "description": "Stable key for this confirmation attempt. Reuse the same key when retrying an identical request. Maximum 128 characters.",
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[A-Za-z0-9._:-]+$"
        },
        "example": "booking-18e83774-65ef-4944-8c8c-b2296ca6605d"
      }
    },
    "schemas": {
      "Business": {
        "type": "object",
        "additionalProperties": false,
        "required": ["businessId", "name", "description", "city", "countryCode"],
        "properties": {
          "businessId": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "description": { "type": "string" },
          "city": { "type": "string" },
          "countryCode": { "type": "string", "pattern": "^[A-Z]{0,2}$" }
        }
      },
      "Service": {
        "type": "object",
        "additionalProperties": false,
        "required": ["serviceId", "businessId", "name", "description", "category", "durationMinutes", "price", "currency"],
        "properties": {
          "serviceId": { "type": "string", "format": "uuid" },
          "businessId": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "description": { "type": "string" },
          "category": { "type": "string" },
          "durationMinutes": { "type": "integer", "minimum": 0 },
          "price": { "type": ["number", "null"], "minimum": 0 },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 }
        }
      },
      "BusinessSearchResponse": {
        "type": "object",
        "additionalProperties": false,
        "required": ["items", "count"],
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/Business" } },
          "count": { "type": "integer", "minimum": 0 }
        }
      },
      "ServiceSearchResponse": {
        "type": "object",
        "additionalProperties": false,
        "required": ["items", "count"],
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/Service" } },
          "count": { "type": "integer", "minimum": 0 }
        }
      },
      "AvailabilitySearchRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["businessId", "serviceId", "dateFrom", "dateTo"],
        "properties": {
          "businessId": { "type": "string", "format": "uuid" },
          "serviceId": { "type": "string", "format": "uuid" },
          "dateFrom": { "type": "string", "format": "date" },
          "dateTo": { "type": "string", "format": "date" },
          "timeAfter": { "type": ["string", "null"], "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
          "timeBefore": { "type": ["string", "null"], "pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]$" },
          "limit": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 }
        }
      },
      "AvailabilitySlot": {
        "type": "object",
        "additionalProperties": false,
        "required": ["slotRef", "businessId", "serviceId", "start", "end", "staffName", "resourceName", "price", "currency", "expiresAt"],
        "properties": {
          "slotRef": { "type": "string", "minLength": 1 },
          "businessId": { "type": "string", "format": "uuid" },
          "serviceId": { "type": "string", "format": "uuid" },
          "start": { "type": "string", "format": "date-time" },
          "end": { "type": "string", "format": "date-time" },
          "staffName": { "type": "string" },
          "resourceName": { "type": "string" },
          "price": { "type": ["number", "null"], "minimum": 0 },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 },
          "expiresAt": { "type": "string", "format": "date-time" }
        }
      },
      "AvailabilitySearchResponse": {
        "type": "object",
        "additionalProperties": false,
        "required": ["items", "count"],
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/AvailabilitySlot" } },
          "count": { "type": "integer", "minimum": 0 }
        }
      },
      "Customer": {
        "type": "object",
        "additionalProperties": false,
        "required": ["firstName", "lastName", "email", "phone"],
        "properties": {
          "firstName": { "type": "string", "minLength": 1, "maxLength": 100 },
          "lastName": { "type": "string", "minLength": 1, "maxLength": 100 },
          "email": { "type": "string", "format": "email", "maxLength": 254 },
          "phone": { "type": "string", "minLength": 5, "maxLength": 40 }
        }
      },
      "PrepareBookingRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["slotRef", "customer"],
        "properties": {
          "slotRef": { "type": "string", "minLength": 1 },
          "customer": { "$ref": "#/components/schemas/Customer" }
        }
      },
      "BookingSummary": {
        "type": "object",
        "additionalProperties": false,
        "required": ["businessId", "serviceId", "businessName", "serviceName", "start", "end", "price", "currency"],
        "properties": {
          "businessId": { "type": "string", "format": "uuid" },
          "serviceId": { "type": "string", "format": "uuid" },
          "businessName": { "type": "string" },
          "serviceName": { "type": "string" },
          "start": { "type": "string", "format": "date-time" },
          "end": { "type": "string", "format": "date-time" },
          "price": { "type": ["number", "null"], "minimum": 0 },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 }
        }
      },
      "PrepareBookingResponse": {
        "type": "object",
        "additionalProperties": false,
        "required": ["intentId", "confirmationRef", "requiresExplicitConfirmation", "expiresAt", "summary"],
        "properties": {
          "intentId": { "type": "string", "format": "uuid" },
          "confirmationRef": { "type": "string", "minLength": 1 },
          "requiresExplicitConfirmation": { "type": "boolean", "const": true },
          "expiresAt": { "type": "string", "format": "date-time" },
          "summary": { "$ref": "#/components/schemas/BookingSummary" }
        }
      },
      "ConfirmBookingRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["confirmationRef", "confirm"],
        "properties": {
          "confirmationRef": { "type": "string", "minLength": 1 },
          "confirm": { "type": "boolean", "const": true }
        }
      },
      "ConfirmBookingResponse": {
        "type": "object",
        "additionalProperties": false,
        "required": ["bookingId", "status", "confirmedAt", "summary"],
        "properties": {
          "bookingId": { "type": "string", "format": "uuid" },
          "status": { "type": "string", "const": "confirmed" },
          "confirmedAt": { "type": "string", "format": "date-time" },
          "summary": { "$ref": "#/components/schemas/BookingSummary" }
        }
      },
      "ErrorEnvelope": {
        "type": "object",
        "additionalProperties": false,
        "required": ["error"],
        "properties": {
          "error": { "$ref": "#/components/schemas/Error" }
        }
      },
      "Error": {
        "type": "object",
        "additionalProperties": false,
        "required": ["code", "message", "retryable", "correlationId"],
        "properties": {
          "code": { "type": "string" },
          "message": { "type": "string" },
          "retryable": { "type": "boolean" },
          "correlationId": { "type": "string" }
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid parameters, payload, explicit confirmation, or idempotency key.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "Unauthorized": {
        "description": "Missing, invalid, or revoked API key.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorEnvelope" },
            "example": {
              "error": {
                "code": "UNAUTHORIZED_CLIENT",
                "message": "API-nyckeln saknas, är ogiltig eller har återkallats.",
                "retryable": false,
                "correlationId": "0HNBEXAMPLE:00000001"
              }
            }
          }
        }
      },
      "Forbidden": {
        "description": "The opaque reference belongs to another API client.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "NotFound": {
        "description": "The business or service is unavailable or has not opted in.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "Conflict": {
        "description": "Slot or price changed, idempotency conflict, or confirmation already in progress.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "Expired": {
        "description": "The slot reference, confirmation reference, or booking intent expired.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "BookingRejected": {
        "description": "Current booking rules rejected the booking.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "RateLimited": {
        "description": "The per-client fixed-window limit was exceeded. The default is 60 requests per minute and can be configured per client.",
        "headers": {
          "Retry-After": {
            "description": "Seconds until the client should retry.",
            "schema": { "type": "integer", "minimum": 1 }
          }
        },
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorEnvelope" },
            "example": {
              "error": {
                "code": "RATE_LIMITED",
                "message": "För många anrop. Försök igen senare.",
                "retryable": true,
                "correlationId": "0HNBEXAMPLE:00000002"
              }
            }
          }
        }
      },
      "TemporarilyUnavailable": {
        "description": "A required booking dependency is temporarily unavailable.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      },
      "InternalError": {
        "description": "Unexpected server error. Use correlationId when contacting support.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } }
      }
    }
  }
}