openapi: 3.1.0
info:
  title: Adapty Mail API
  version: 1.0.0
  description: |
    The Adapty Mail API lets you send user profiles and transaction events to Adapty Mail directly
    from your server, without routing the data through the Adapty SDK.

    Use it to:

    - Add subscribers when you don't have a base in Adapty Mail yet.
    - Reuse the subscriber base from your other apps.
    - Feed Adapty Mail server-to-server, with your backend as the source of truth.

    A profile with an email is enough for the **never purchased** flow. Every other flow
    (renewal cancelled, billing issue, expired, refunded) is driven by purchase history, so those
    profiles also need transaction events to be placed in the right flow.

    For a step-by-step walkthrough, see [Send emails and transactions via the Adapty Mail API](/docs/mail-send-data-via-api).
servers:
  - url: https://api-mail.adapty.io
    description: Production server
paths:
  /api/v1/profile/save/:
    post:
      summary: Save profile
      description: |
        Creates or updates a profile in Adapty Mail. A profile carries the user's email and attributes
        that Adapty Mail uses to identify recipients and build [segments](/docs/mail-segments).

        Identify each user by a stable `external_profile_id`. Sending the same `external_profile_id`
        again updates the existing profile rather than creating a duplicate.
      operationId: saveProfile
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProfileDTO'
            examples:
              basic:
                summary: Profile with an email, ready for the "never purchased" flow
                value:
                  external_profile_id: "user_12345"
                  external_created_at: "2026-06-01T10:30:00Z"
                  email: "jane@example.com"
                  country: "US"
                  custom_attributes:
                    plan: "trial"
      responses:
        '200':
          description: Profile saved successfully. The response body is an empty object.
          content:
            application/json:
              schema:
                type: object
              examples:
                default:
                  value: {}
        '400':
          description: Validation failed — a required field is missing or invalid. `field_name` shows which field.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "Field required"
                        error_code: "base_error"
                        status_code: 400
                        field_name: "email"
        '403':
          description: Missing or invalid secret API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "Secret key doesn't exist"
                        error_code: "secret_key_does_not_exist_error"
                        status_code: 403
                        field_name: null
  /api/v1/profile/delete/:
    post:
      summary: Delete profile
      description: |
        Erases a profile's personal data and cancels its scheduled emails. Use it to fulfil a
        right-to-erasure request.

        Identify the profile by `external_profile_id`, `customer_user_id`, or `email` — send at
        least one. Adapty Mail matches people by email address, so an `email` you send is the only
        identifier tried; leave it out and the `external_profile_id` or `customer_user_id` finds
        the profile instead.

        Deletion clears the stored data, but it doesn't block the address. A later
        [Save profile](#operation/saveProfile) request creates a new profile, with none of the
        deleted one's history. To keep someone out for good, stop sending their data.

        Repeat a deletion by `external_profile_id` or `customer_user_id` and it succeeds again
        without changing anything. Repeat one that carried only `email` and it returns `404`:
        erasure clears the stored address, so an email has nothing left to match.
      operationId: deleteProfile
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProfileDeleteDTO'
            examples:
              byExternalProfileId:
                summary: Delete by the identifier you sent when saving the profile
                value:
                  external_profile_id: "user_12345"
              byEmail:
                summary: Delete by email address
                value:
                  email: "jane@example.com"
      responses:
        '204':
          description: Profile deleted. The response has no body.
        '400':
          description: No identifier was provided.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "At least one of external_profile_id, customer_user_id or email must be provided"
                        error_code: "missing_identifier"
                        status_code: 400
                        field_name: null
        '403':
          description: Missing or invalid secret API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "Secret key doesn't exist"
                        error_code: "secret_key_does_not_exist_error"
                        status_code: 403
                        field_name: null
        '404':
          description: No profile matches the identifiers you sent.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "Profile not found"
                        error_code: "profile_not_found_error"
                        status_code: 404
                        field_name: null
  /api/v1/profile/transaction-event/save/:
    post:
      summary: Save transaction event
      description: |
        Records a store transaction event for a profile. Adapty Mail uses transaction events to place
        profiles in purchase-based flows — the `event_type` maps to flows such as renewal cancelled,
        billing issue, expired, and refunded — and for revenue attribution.

        Send these events as you handle purchases, renewals, and cancellations. Only the
        **never purchased** flow works without them.
      operationId: saveTransactionEvent
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TransactionEventDTO'
            examples:
              basic:
                summary: A new monthly subscription purchase
                value:
                  event_type: "subscription_started"
                  event_id: "evt_abc123"
                  event_datetime: "2026-06-10T14:20:05Z"
                  external_profile_id: "user_12345"
                  store: "app_store"
                  store_product_id: "premium_monthly"
                  store_transaction_id: "1000000123456789"
                  store_original_transaction_id: "1000000123456789"
                  purchased_at: "2026-06-10T14:20:00Z"
                  originally_purchased_at: "2026-06-10T14:20:00Z"
                  price_usd: "9.99"
                  expires_at: "2026-07-10T14:20:00Z"
      responses:
        '200':
          description: Transaction event saved successfully. The response body is an empty object.
          content:
            application/json:
              schema:
                type: object
              examples:
                default:
                  value: {}
        '400':
          description: Validation failed — a required field is missing or invalid. `field_name` shows which field.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "Field required"
                        error_code: "base_error"
                        status_code: 400
                        field_name: "event_type"
        '403':
          description: Missing or invalid secret API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Errors'
              examples:
                default:
                  value:
                    errors:
                      - message: "Secret key doesn't exist"
                        error_code: "secret_key_does_not_exist_error"
                        status_code: 403
                        field_name: null
components:
  securitySchemes:
    apikeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: |
        Authenticate every request with your Adapty Mail secret API key, sent as the **Authorization**
        header with the value `Bearer {your_secret_api_key}`, for example, `Bearer secret_live_...`.

        Find this key in Adapty Mail under **Settings**. The key is project-specific — it identifies the
        project the data belongs to, so the profile and transaction endpoints don't take a project ID.
  schemas:
    ProfileDTO:
      type: object
      required:
        - external_profile_id
        - external_created_at
        - email
      properties:
        external_profile_id:
          type: string
          description: |
            Stable identifier for the user, owned by your app or backend. Reuse the same value across
            requests so Adapty Mail links emails, clicks, and purchases to one profile. Never use an
            anonymous or per-install identifier.
        external_created_at:
          type: string
          format: date-time
          description: |
            The user creation time, in ISO 8601 format (for example, `"2026-06-01T10:30:00Z"`).
            You can use this date in segments.
        email:
          type: string
          format: email
          description: |
            The user's email address. Adapty Mail delivers campaigns to this address.

            The address is write-once: it is set when the profile is created and later saves don't
            overwrite it.
        customer_user_id:
          type: string
          description: |
            The user's identifier in your own system, if you have one that Adapty also knows. Adapty
            Mail uses it to recognize a person who also reaches it through the Adapty SDK, so the two
            sources share one profile instead of producing a duplicate that mails the same person twice.
        first_name:
          type: string
          description: The user's first name.
        last_name:
          type: string
          description: The user's last name.
        gender:
          type: string
          description: The user's gender.
        birthday:
          type: string
          format: date
          description: The user's date of birth, in ISO 8601 format (for example, `"1990-05-21"`).
        country:
          type: string
          description: The user's country as a two-letter ISO 3166-1 alpha-2 code (for example, `US`).
        store_country:
          type: string
          description: The user's store region as a two-letter ISO 3166-1 alpha-2 code (for example, `US`).
        email_consent:
          type: boolean
          description: |
            The user's answer to receiving marketing email: `true` if they agreed, `false` if they declined.
            Adapty Mail sends no marketing email to a profile whose answer is `false`, and no
            [double opt-in](/docs/mail-double-opt-in) confirmation request either. Other transactional
            emails still reach it.

            Omit the field to keep the stored answer. A profile without an answer receives marketing email.
        custom_attributes:
          type: object
          description: |
            Arbitrary key-value pairs (string or number values) to attach to the profile. Use them to
            build [segments](/docs/mail-segments) — for example, `plan`, `signup_source`, or `trial_days`.
        device_info:
          $ref: '#/components/schemas/DeviceInfoDTO'
    ProfileDeleteDTO:
      type: object
      description: |
        Identifiers for the profile to erase. Send at least one of `external_profile_id`,
        `customer_user_id`, or `email`.
      anyOf:
        - required: [external_profile_id]
        - required: [customer_user_id]
        - required: [email]
      properties:
        external_profile_id:
          type: string
          description: The identifier you sent when saving the profile.
        customer_user_id:
          type: string
          description: The user's identifier in your own system.
        email:
          type: string
          format: email
          description: The email address on the profile.
    DeviceInfoDTO:
      type: object
      required:
        - platform
      properties:
        platform:
          type: string
          description: The platform the user is on, for example, `iOS` or `Android`.
        device:
          type: string
          description: Device model, for example, `iPhone15,2`.
        os:
          type: string
          description: Operating system version, for example, `17.5`.
        locale:
          type: string
          description: The user's locale, for example, `en-US`. Adapty Mail uses it to decide which translation of a [localized campaign](/docs/mail-localization#select-languages) to send. A user with no locale receives the English emails.
        timezone:
          type: string
          description: The user's timezone, for example, `America/New_York`.
        app_version:
          type: string
          description: Version of your app the user is running, for example, `3.1.0`.
    TransactionEventDTO:
      type: object
      required:
        - event_type
        - event_id
        - event_datetime
        - external_profile_id
        - store
        - store_product_id
        - store_transaction_id
        - store_original_transaction_id
        - purchased_at
        - originally_purchased_at
      properties:
        event_type:
          $ref: '#/components/schemas/TransactionEventType'
        event_id:
          type: string
          description: Unique identifier for this event, owned by your system. Use it to keep events idempotent.
        event_datetime:
          type: string
          format: date-time
          description: When the event was recorded, in ISO 8601 format.
        external_profile_id:
          type: string
          description: |
            The same stable `external_profile_id` you send when saving the profile. Links the
            transaction to the right profile.

            You can send a transaction before the profile exists. Adapty Mail stores the event
            unattached and links it to the profile on the next save that carries the same identifier.
        customer_user_id:
          type: string
          description: |
            The user's identifier in your own system. Adapty Mail uses it to find the profile when
            `external_profile_id` doesn't match one. It isn't stored on the event.
        email:
          type: string
          format: email
          description: |
            The buyer's email address. Adapty Mail uses it to find the profile when neither
            identifier matches one. It isn't stored on the event.
        store:
          type: string
          description: The store the transaction came from, for example, `app_store`, `play_store`, or `stripe`.
        store_product_id:
          type: string
          description: Identifier of the purchased product in the store.
        store_transaction_id:
          type: string
          description: Identifier of this transaction in the store.
        store_original_transaction_id:
          type: string
          description: Identifier of the first transaction in the subscription chain. For the first purchase, this equals `store_transaction_id`.
        purchased_at:
          type: string
          format: date-time
          description: When this transaction occurred, in ISO 8601 format.
        originally_purchased_at:
          type: string
          format: date-time
          description: When the subscription was first purchased, in ISO 8601 format.
        price_usd:
          type: string
          description: The transaction amount in USD, as a decimal string (for example, `"9.99"`).
        expires_at:
          type: string
          format: date-time
          description: When the subscription expires or expired, in ISO 8601 format. Omit for non-subscription purchases.
        offer:
          $ref: '#/components/schemas/Offer'
    TransactionEventType:
      type: string
      description: The kind of transaction event. Purchase-based flows are triggered by these values.
      enum:
        - subscription_started
        - subscription_renewed
        - subscription_renewal_cancelled
        - subscription_renewal_reactivated
        - billing_issue_detected
        - entered_grace_period
        - subscription_refunded
        - subscription_expired
        - non_subscription_purchase
        - non_subscription_purchase_refunded
    Offer:
      type: object
      description: Details of a promotional or introductory offer applied to the transaction.
      required:
        - category
        - offer_type
      properties:
        category:
          $ref: '#/components/schemas/OfferCategory'
        offer_type:
          $ref: '#/components/schemas/OfferType'
        offer_id:
          type: string
          description: Identifier of the offer in the store, if any.
    OfferCategory:
      type: string
      enum:
        - introductory
        - promotional
        - offer_code
        - win_back
    OfferType:
      type: string
      enum:
        - free_trial
        - pay_as_you_go
        - pay_up_front
    Errors:
      type: object
      description: Standard error response. Every failure returns a 4XX status with this shape.
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              message:
                type: string
                description: Human-readable description of the error.
              error_code:
                type: string
                description: Machine-readable error identifier.
              status_code:
                type: integer
                description: HTTP status code for this error.
              field_name:
                type: string
                description: The request field that caused the error, or `null` if the error isn't field-specific.
