{
  "openapi": "3.1.0",
  "info": {
    "title": "TunedReach API",
    "version": "1.0.0",
    "description": "Send contacts into TunedReach, read suppressions and audience counts, and manage webhooks. Docs: https://tunedreach.com/developers",
    "contact": {
      "email": "hello@tunedreach.com",
      "url": "https://tunedreach.com/developers"
    }
  },
  "servers": [
    {
      "url": "https://tunedreach.com/api/v1"
    }
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "paths": {
    "/customers": {
      "post": {
        "operationId": "upsertCustomers",
        "summary": "Add or update up to 500 contacts",
        "description": "Scope contacts:write. Upsert by email.",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CustomersRequest"
              },
              "example": {
                "customers": [
                  {
                    "email": "ada@example.com",
                    "firstName": "Ada",
                    "lastName": "Okafor",
                    "consent_basis": "consent",
                    "tags_add": [
                      "signup:website"
                    ],
                    "externalId": "user_1001"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Counts per outcome",
            "headers": {
              "$ref": "#/components/headers/RateLimit"
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CustomersResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "413": {
            "$ref": "#/components/responses/Error"
          },
          "415": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/contacts": {
      "get": {
        "operationId": "getContacts",
        "summary": "Look up one contact by email, or list contacts",
        "description": "Scope contacts:read.",
        "parameters": [
          {
            "name": "email",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "email"
            }
          },
          {
            "name": "tag",
            "in": "query",
            "schema": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9:_.-]{0,39}$"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "One contact (email given) or a page",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "contact"
                      ],
                      "properties": {
                        "contact": {
                          "$ref": "#/components/schemas/Contact"
                        }
                      }
                    },
                    {
                      "type": "object",
                      "required": [
                        "contacts",
                        "has_more",
                        "next_cursor"
                      ],
                      "properties": {
                        "contacts": {
                          "type": "array",
                          "items": {
                            "$ref": "#/components/schemas/Contact"
                          }
                        },
                        "has_more": {
                          "type": "boolean"
                        },
                        "next_cursor": {
                          "type": [
                            "string",
                            "null"
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/suppressions": {
      "get": {
        "operationId": "getSuppressions",
        "summary": "Addresses that must not be emailed",
        "description": "Scope suppressions:read. Oldest first, cursor-paged.",
        "parameters": [
          {
            "name": "since",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 1000,
              "default": 500
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "A page of suppressions",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "suppressions",
                    "has_more",
                    "next_cursor"
                  ],
                  "properties": {
                    "suppressions": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Suppression"
                      }
                    },
                    "has_more": {
                      "type": "boolean"
                    },
                    "next_cursor": {
                      "type": [
                        "string",
                        "null"
                      ]
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/audience": {
      "get": {
        "operationId": "getAudience",
        "summary": "How many contacts with a tag can be emailed",
        "description": "Scope audience:read.",
        "parameters": [
          {
            "name": "tag",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9:_.-]{0,39}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Count",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "tag",
                    "emailable"
                  ],
                  "properties": {
                    "tag": {
                      "type": "string"
                    },
                    "emailable": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/webhooks": {
      "get": {
        "operationId": "listWebhooks",
        "summary": "List webhook endpoints",
        "description": "Scope webhooks.",
        "responses": {
          "200": {
            "description": "Endpoints",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "webhooks": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Webhook"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      },
      "post": {
        "operationId": "createWebhook",
        "summary": "Add a webhook endpoint (the secret is returned once)",
        "description": "Scope webhooks. At most 5 per workspace.",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url",
                  "events"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri",
                    "pattern": "^https://"
                  },
                  "events": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "$ref": "#/components/schemas/EventType"
                    }
                  },
                  "include_reply_body": {
                    "type": "boolean",
                    "default": false
                  },
                  "description": {
                    "type": "string",
                    "maxLength": 100
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "webhook": {
                      "$ref": "#/components/schemas/Webhook"
                    },
                    "secret": {
                      "type": [
                        "string",
                        "null"
                      ],
                      "description": "whsec_…; null on an idempotent replay"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/webhooks/{id}": {
      "delete": {
        "operationId": "deleteWebhook",
        "summary": "Delete a webhook endpoint",
        "description": "Scope webhooks.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Deleted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "deleted": {
                      "type": "boolean"
                    },
                    "id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/key": {
      "get": {
        "operationId": "getKey",
        "summary": "What this key is: workspace, scopes, rate limit",
        "responses": {
          "200": {
            "description": "Key info",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "workspace_id": {
                      "type": "string"
                    },
                    "integration_id": {
                      "type": "string"
                    },
                    "kind": {
                      "enum": [
                        "custom",
                        "tunedbooks"
                      ]
                    },
                    "scopes": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Scope"
                      }
                    },
                    "rate_limit_per_minute": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    }
  },
  "webhooks": {
    "contact.created": {
      "post": {
        "summary": "A contact was added (API, import, form, prospect finder or the app).",
        "parameters": [
          {
            "name": "TunedReach-Signature",
            "in": "header",
            "required": true,
            "description": "t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<raw body>'>",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Event",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Delivery",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Event"
              }
            }
          }
        },
        "responses": {
          "2XX": {
            "description": "Received. Anything else is retried for 24 hours."
          }
        }
      }
    },
    "contact.unsubscribed": {
      "post": {
        "summary": "A contact unsubscribed or was opted out.",
        "parameters": [
          {
            "name": "TunedReach-Signature",
            "in": "header",
            "required": true,
            "description": "t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<raw body>'>",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Event",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Delivery",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Event"
              }
            }
          }
        },
        "responses": {
          "2XX": {
            "description": "Received. Anything else is retried for 24 hours."
          }
        }
      }
    },
    "contact.bounced": {
      "post": {
        "summary": "An email to the contact bounced; they won't be emailed again.",
        "parameters": [
          {
            "name": "TunedReach-Signature",
            "in": "header",
            "required": true,
            "description": "t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<raw body>'>",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Event",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Delivery",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Event"
              }
            }
          }
        },
        "responses": {
          "2XX": {
            "description": "Received. Anything else is retried for 24 hours."
          }
        }
      }
    },
    "contact.complained": {
      "post": {
        "summary": "The contact marked an email as spam.",
        "parameters": [
          {
            "name": "TunedReach-Signature",
            "in": "header",
            "required": true,
            "description": "t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<raw body>'>",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Event",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Delivery",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Event"
              }
            }
          }
        },
        "responses": {
          "2XX": {
            "description": "Received. Anything else is retried for 24 hours."
          }
        }
      }
    },
    "reply.received": {
      "post": {
        "summary": "A contact replied. Includes the AI label when there is one; the reply text only if you opt in.",
        "parameters": [
          {
            "name": "TunedReach-Signature",
            "in": "header",
            "required": true,
            "description": "t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<raw body>'>",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Event",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Delivery",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Event"
              }
            }
          }
        },
        "responses": {
          "2XX": {
            "description": "Received. Anything else is retried for 24 hours."
          }
        }
      }
    },
    "sequence.completed": {
      "post": {
        "summary": "A contact received the last email of a sequence.",
        "parameters": [
          {
            "name": "TunedReach-Signature",
            "in": "header",
            "required": true,
            "description": "t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<raw body>'>",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Event",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "TunedReach-Delivery",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Event"
              }
            }
          }
        },
        "responses": {
          "2XX": {
            "description": "Received. Anything else is retried for 24 hours."
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "trk_live_…"
      }
    },
    "parameters": {
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "description": "Replays the first answer for 24 hours.",
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 255
        }
      }
    },
    "headers": {
      "RateLimit": {
        "description": "X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset (unix seconds)",
        "schema": {
          "type": "string"
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "RateLimited": {
        "description": "Too many requests",
        "headers": {
          "Retry-After": {
            "schema": {
              "type": "integer"
            }
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Scope": {
        "enum": [
          "contacts:write",
          "contacts:read",
          "suppressions:read",
          "audience:read",
          "webhooks"
        ]
      },
      "EventType": {
        "enum": [
          "contact.created",
          "contact.unsubscribed",
          "contact.bounced",
          "contact.complained",
          "reply.received",
          "sequence.completed"
        ]
      },
      "Error": {
        "type": "object",
        "required": [
          "error",
          "message",
          "request_id"
        ],
        "properties": {
          "error": {
            "enum": [
              "invalid_body",
              "invalid_parameter",
              "idempotency_key_invalid",
              "missing_or_malformed_key",
              "invalid_or_revoked_key",
              "insufficient_scope",
              "not_found",
              "unsupported_version",
              "resource_not_found",
              "method_not_allowed",
              "idempotency_in_progress",
              "too_many_customers",
              "unsupported_media_type",
              "idempotency_key_reused",
              "rate_limited",
              "internal_error"
            ]
          },
          "message": {
            "type": "string"
          },
          "request_id": {
            "type": "string"
          },
          "param": {
            "type": "string"
          },
          "required_scope": {
            "type": "string"
          },
          "retry_after": {
            "type": "integer"
          }
        }
      },
      "Customer": {
        "type": "object",
        "required": [
          "email"
        ],
        "properties": {
          "email": {
            "type": "string",
            "format": "email"
          },
          "consent_basis": {
            "enum": [
              "consent",
              "existing_customer",
              "prospect"
            ],
            "description": "Required for API keys (TunedBooks keys default to existing_customer)."
          },
          "externalId": {
            "type": "string",
            "maxLength": 100
          },
          "firstName": {
            "type": "string"
          },
          "lastName": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "phone": {
            "type": "string"
          },
          "marketing_opt_in": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "tags_add": {
            "type": "array",
            "maxItems": 10,
            "items": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9:_.-]{0,39}$"
            }
          },
          "tags_remove": {
            "type": "array",
            "maxItems": 10,
            "items": {
              "type": "string"
            }
          },
          "attributes": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "stage": {
                "enum": [
                  "signed_up",
                  "activated",
                  "first_value",
                  "paying",
                  "retained",
                  "at_risk",
                  "churned",
                  "won_back"
                ]
              },
              "plan": {
                "type": "string"
              },
              "country": {
                "type": "string",
                "pattern": "^[A-Za-z]{2}$"
              },
              "industry": {
                "type": "string"
              }
            }
          },
          "lastPurchaseAt": {
            "type": "string",
            "format": "date-time"
          },
          "firstPurchaseAt": {
            "type": "string",
            "format": "date-time"
          },
          "totalSpent": {
            "type": "number"
          },
          "orderCount": {
            "type": "integer",
            "minimum": 0
          },
          "currency": {
            "type": "string",
            "pattern": "^[A-Z]{3}$"
          }
        }
      },
      "CustomersRequest": {
        "type": "object",
        "required": [
          "customers"
        ],
        "properties": {
          "customers": {
            "type": "array",
            "minItems": 1,
            "maxItems": 500,
            "items": {
              "$ref": "#/components/schemas/Customer"
            }
          }
        }
      },
      "CustomersResult": {
        "type": "object",
        "properties": {
          "received": {
            "type": "integer"
          },
          "created": {
            "type": "integer"
          },
          "updated": {
            "type": "integer"
          },
          "optedOut": {
            "type": "integer"
          },
          "suppressed": {
            "type": "integer"
          },
          "invalid": {
            "type": "integer"
          },
          "errors": {
            "type": "array",
            "maxItems": 20,
            "items": {
              "type": "object",
              "properties": {
                "index": {
                  "type": "integer"
                },
                "error": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "Contact": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "email": {
            "type": "string"
          },
          "first_name": {
            "type": "string"
          },
          "last_name": {
            "type": "string"
          },
          "company": {
            "type": "string"
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "status": {
            "enum": [
              "active",
              "replied",
              "unsubscribed",
              "bounced",
              "complained"
            ]
          },
          "consent_basis": {
            "enum": [
              "consent",
              "existing_customer",
              "prospect",
              null
            ]
          },
          "source": {
            "type": [
              "string",
              "null"
            ]
          },
          "fields": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "created_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "updated_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          }
        }
      },
      "Suppression": {
        "type": "object",
        "properties": {
          "email": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          },
          "at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          }
        }
      },
      "Webhook": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "url": {
            "type": "string"
          },
          "events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/EventType"
            }
          },
          "include_reply_body": {
            "type": "boolean"
          },
          "description": {
            "type": "string"
          },
          "active": {
            "type": "boolean"
          },
          "secret_preview": {
            "type": [
              "string",
              "null"
            ]
          },
          "failures": {
            "type": "integer"
          },
          "last_status": {
            "type": [
              "integer",
              "null"
            ]
          },
          "last_error": {
            "type": [
              "string",
              "null"
            ]
          },
          "last_delivery_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "created_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "created_by": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "Event": {
        "type": "object",
        "required": [
          "id",
          "type",
          "created",
          "workspace_id",
          "data"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "created": {
            "type": "string",
            "format": "date-time"
          },
          "workspace_id": {
            "type": "string"
          },
          "data": {
            "type": "object"
          }
        }
      }
    }
  }
}
