# Set transaction

> Creates a new transaction for an end user of your app in Adapty and provides access level. The transaction created by this method will appear in your analytics and Event Feed and will be sent to all integrations.

## OpenAPI

```yaml
/api-specs/adapty-api.yaml post /api/v2/server-side-api/purchase/set/transaction/
openapi: 3.1.0
info:
  title: Adapty server-side API
  version: 1.0.0
servers:
  - url: https://api.adapty.io
    description: Production server
paths:
  /api/v2/server-side-api/purchase/set/transaction/:
    post:
      summary: Set transaction
      description: Creates a new transaction for an end user of your app in Adapty and provides access level. The transaction created by this method will appear in your analytics and Event Feed and will be sent to all integrations.
      operationId: setTransaction
      tags:
        - Purchase
      security:
        - apikeyAuth: []
      parameters:
        - name: adapty-customer-user-id
          in: header
          required: false
          schema:
            type: string
          description: The unique ID of the customer in your system. Either `adapty-customer-user-id` or `adapty-profile-id` is required.
        - name: adapty-profile-id
          in: header
          required: false
          schema:
            type: string
          description: The unique ID of the profile in your system. Your best option if you are working with anonymous profiles. Either `adapty-customer-user-id` or `adapty-profile-id` is required.
      responses:
        "200":
          description: Transaction recorded successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ProfileResponse"
        "400":
          description: Bad request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "401":
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "404":
          description: Profile not found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "500":
          description: Internal server error
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TransactionDTORequest"
            examples:
              one_time_purchase:
                summary: One-time purchase example
                value:
                  purchase_type: one_time_purchase
                  store: app_store
                  environment: Production
                  store_product_id: premium_lifetime
                  store_transaction_id: "1000000123456789"
                  store_original_transaction_id: "1000000123456789"
                  is_family_shared: false
                  price:
                    country: US
                    currency: USD
                    value: 9.99
                  purchased_at: "2024-01-15T10:30:00Z"
                  variation_id: variation_123
                  offer: null
                  refunded_at: null
                  cancellation_reason: null
              subscription:
                summary: Subscription example
                value:
                  purchase_type: subscription
                  store: app_store
                  environment: Production
                  store_product_id: premium_monthly
                  store_transaction_id: "1000000123456789"
                  store_original_transaction_id: "1000000123456789"
                  is_family_shared: false
                  price:
                    country: US
                    currency: USD
                    value: 4.99
                  purchased_at: "2024-01-15T10:30:00Z"
                  originally_purchased_at: "2024-01-15T10:30:00Z"
                  expires_at: "2024-02-15T10:30:00Z"
                  renew_status: true
                  renew_status_changed_at: null
                  billing_issue_detected_at: null
                  grace_period_expires_at: null
                  variation_id: variation_123
                  offer:
                    category: introductory
                    type: free_trial
                    id: trial_offer_123
                  refunded_at: null
                  cancellation_reason: null
components:
  schemas:
    ProfileResponse:
      type: object
      properties:
        data:
          $ref: "#/components/schemas/Profile"
      required:
        - data
    ErrorResponse:
      type: object
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              source:
                type: string
                nullable: true
                description: Source of the error
              errors:
                type: array
                items:
                  type: string
                description: Array of error messages
        error_code:
          type: string
          description: Short error name
        status_code:
          type: integer
          description: HTTP status code
      required:
        - errors
        - error_code
        - status_code
    TransactionDTORequest:
      oneOf:
        - $ref: "#/components/schemas/OneTimePurchaseDTO"
        - $ref: "#/components/schemas/SubscriptionDTO"
      discriminator:
        propertyName: purchase_type
    Profile:
      type: object
      properties:
        app_id:
          type: string
          format: uuid
          description: The internal ID of your app
        profile_id:
          type: string
          format: uuid
          description: Adapty profile ID
        customer_user_id:
          type: string
          nullable: true
          description: The ID of your user in your system
        total_revenue_usd:
          type: number
          format: float
          description: A float value representing the total revenue in USD earned in the profile
        segment_hash:
          type: string
          description: Internal parameter
        timestamp:
          type: integer
          format: int64
          description: Response time in milliseconds, needs for resolve a race condition
        custom_attributes:
          type: array
          items:
            $ref: "#/components/schemas/CustomAttribute"
          description: A maximum of 30 custom attributes to the profile are allowed to be set
        access_levels:
          type: array
          items:
            $ref: "#/components/schemas/AccessLevel"
          description: Array of Access level objects. Empty array if the customer has no access levels
        subscriptions:
          type: array
          items:
            $ref: "#/components/schemas/Subscription"
          description: Array of Subscription objects. Empty array if the customer has no subscriptions
        non_subscriptions:
          type: array
          items:
            $ref: "#/components/schemas/NonSubscription"
          description: Array of Non-Subscription objects. Empty array if the customer has no purchases
      required:
        - app_id
        - profile_id
        - customer_user_id
        - total_revenue_usd
        - segment_hash
        - timestamp
        - custom_attributes
        - access_levels
        - subscriptions
        - non_subscriptions
    OneTimePurchaseDTO:
      type: object
      title: One-time Purchase
      description: Transaction data for a one-time purchase
      properties:
        purchase_type:
          type: string
          enum:
            - one_time_purchase
          description: Type of purchase
        store:
          type: string
          description: Store where the purchase was made. Common values include `app_store`, `play_store`, `stripe`, `paddle`, or any custom store identifier
        environment:
          type: string
          enum:
            - Production
            - Sandbox
          default: Production
          description: Environment where the purchase was made
        store_product_id:
          type: string
          description: Product ID in the store
        store_transaction_id:
          type: string
          description: Transaction ID in the store
        store_original_transaction_id:
          type: string
          description: Original transaction ID in the store
        is_family_shared:
          type: boolean
          default: false
          description: Whether the purchase is family shared
        price:
          $ref: "#/components/schemas/PriceDTO"
        purchased_at:
          type: string
          format: date-time
          description: When the purchase was made
        variation_id:
          type: string
          nullable: true
          description: Variation ID for A/B testing
        offer:
          $ref: "#/components/schemas/OfferDTO"
          nullable: true
        refunded_at:
          type: string
          format: date-time
          nullable: true
          description: When the purchase was refunded
        cancellation_reason:
          type: string
          enum:
            - billing_error
            - cancelled_by_developer
            - new_subscription_replace
            - price_increase
            - product_was_not_available
            - refund
            - unknown
            - upgraded
            - voluntarily_cancelled
            - adapty_revoked
          nullable: true
          description: Reason for cancellation
      required:
        - purchase_type
        - store
        - store_product_id
        - store_transaction_id
        - store_original_transaction_id
        - price
        - purchased_at
    SubscriptionDTO:
      type: object
      title: Subscription
      description: Transaction data for a subscription purchase
      properties:
        purchase_type:
          type: string
          enum:
            - subscription
          description: Type of purchase
        store:
          type: string
          description: Store where the purchase was made. Common values include `app_store`, `play_store`, `stripe`, `paddle`, or any custom store identifier
        environment:
          type: string
          enum:
            - Production
            - Sandbox
          default: Production
          description: Environment where the purchase was made
        store_product_id:
          type: string
          description: Product ID in the store
        store_transaction_id:
          type: string
          description: Transaction ID in the store
        store_original_transaction_id:
          type: string
          description: Original transaction ID in the store
        is_family_shared:
          type: boolean
          default: false
          description: Whether the purchase is family shared
        price:
          $ref: "#/components/schemas/PriceDTO"
        purchased_at:
          type: string
          format: date-time
          description: When the purchase was made
        variation_id:
          type: string
          nullable: true
          description: Variation ID for A/B testing
        offer:
          $ref: "#/components/schemas/OfferDTO"
          nullable: true
        refunded_at:
          type: string
          format: date-time
          nullable: true
          description: When the purchase was refunded
        cancellation_reason:
          type: string
          enum:
            - billing_error
            - cancelled_by_developer
            - new_subscription_replace
            - price_increase
            - product_was_not_available
            - refund
            - unknown
            - upgraded
            - voluntarily_cancelled
            - adapty_revoked
          nullable: true
          description: Reason for cancellation
        originally_purchased_at:
          type: string
          format: date-time
          description: When the subscription was originally purchased
        expires_at:
          type: string
          format: date-time
          description: When the subscription expires
        renew_status:
          type: boolean
          description: Whether the subscription will renew
        renew_status_changed_at:
          type: string
          format: date-time
          nullable: true
          description: When the renewal status was changed
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: When billing issue was detected
        grace_period_expires_at:
          type: string
          format: date-time
          nullable: true
          description: When grace period expires
      required:
        - purchase_type
        - store
        - store_product_id
        - store_transaction_id
        - store_original_transaction_id
        - price
        - purchased_at
        - originally_purchased_at
        - expires_at
        - renew_status
    CustomAttribute:
      type: object
      properties:
        key:
          type: string
          maxLength: 30
          description: The key must be a string with no more than 30 characters. Only letters, numbers, dashes, points, and underscores allowed
        value:
          oneOf:
            - type: string
            - type: number
          description: The attribute value must be no more than 50 characters. Only strings and floats are allowed as values
      required:
        - key
        - value
    AccessLevel:
      type: object
      properties:
        access_level_id:
          type: string
          description: Access level identifier
        store:
          type: string
          description: Store where the access level was purchased
        store_product_id:
          type: string
          description: Product ID in the store
        store_base_plan_id:
          type: string
          nullable: true
          description: Base plan ID in the store
        store_transaction_id:
          type: string
          description: Transaction ID in the store
        store_original_transaction_id:
          type: string
          description: Original transaction ID in the store
        offer:
          allOf:
            - $ref: "#/components/schemas/OfferDTO"
          nullable: true
          description: Offer details, if a promotional or introductory offer was applied
        starts_at:
          type: string
          format: date-time
          nullable: true
          description: When the access level starts
        purchased_at:
          type: string
          format: date-time
          description: When the access level was purchased
        originally_purchased_at:
          type: string
          format: date-time
          description: When the access level was originally purchased
        expires_at:
          type: string
          format: date-time
          nullable: true
          description: When the access level expires
        renewal_cancelled_at:
          type: string
          format: date-time
          nullable: true
          description: When renewal was cancelled
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: When billing issue was detected
        is_in_grace_period:
          type: boolean
          description: Whether the access level is in grace period
        cancellation_reason:
          type: string
          nullable: true
          description: Reason for cancellation
    Subscription:
      type: object
      properties:
        store:
          type: string
          description: Store where the subscription was purchased
        store_product_id:
          type: string
          description: Product ID in the store
        store_base_plan_id:
          type: string
          nullable: true
          description: Base plan ID in the store
        store_transaction_id:
          type: string
          description: Transaction ID in the store
        store_original_transaction_id:
          type: string
          description: Original transaction ID in the store
        offer:
          allOf:
            - $ref: "#/components/schemas/OfferDTO"
          nullable: true
          description: Offer details, if a promotional or introductory offer was applied
        environment:
          type: string
          description: Environment (Sandbox, Production)
        purchased_at:
          type: string
          format: date-time
          description: When the subscription was purchased
        originally_purchased_at:
          type: string
          format: date-time
          description: When the subscription was originally purchased
        expires_at:
          type: string
          format: date-time
          nullable: true
          description: When the subscription expires
        renewal_cancelled_at:
          type: string
          format: date-time
          nullable: true
          description: When renewal was cancelled
        billing_issue_detected_at:
          type: string
          format: date-time
          nullable: true
          description: When billing issue was detected
        is_in_grace_period:
          type: boolean
          description: Whether the subscription is in grace period
        cancellation_reason:
          type: string
          nullable: true
          description: Reason for cancellation
    NonSubscription:
      type: object
      properties:
        purchase_id:
          type: string
          format: uuid
          description: Unique purchase identifier
        store:
          type: string
          description: Store where the purchase was made
        store_product_id:
          type: string
          description: Product ID in the store
        store_base_plan_id:
          type: string
          nullable: true
          description: Base plan ID in the store
        store_transaction_id:
          type: string
          description: Transaction ID in the store
        store_original_transaction_id:
          type: string
          description: Original transaction ID in the store
        purchased_at:
          type: string
          format: date-time
          description: When the purchase was made
        environment:
          type: string
          description: Environment (Sandbox, Production)
        is_refund:
          type: boolean
          description: Whether this is a refund
        is_consumable:
          type: boolean
          description: Whether this is a consumable purchase
    PriceDTO:
      type: object
      properties:
        country:
          type: string
          description: Country code
        currency:
          type: string
          description: Currency code
        value:
          type: number
          format: float
          description: Price value
      required:
        - country
        - currency
        - value
    OfferDTO:
      type: object
      properties:
        category:
          type: string
          enum:
            - introductory
            - promotional
            - offer_code
            - win_back
          description: Offer category
        type:
          type: string
          enum:
            - free_trial
            - pay_as_you_go
            - pay_up_front
          description: Offer type
        id:
          type: string
          nullable: true
          description: Offer ID
      required:
        - category
        - type
  securitySchemes:
    apikeyAuth:
      type: apiKey
      name: Authorization
      in: header
      default: Api-Key {Your secret API key}
      description: |
        API requests must be authenticated by your secret API key as the **Authorization** 
        header with the value `Api-Key {your_secret_api_key}`, for example, 
        `Api-Key secret_live_...`. Find this key in the Adapty Dashboard -> 
        **App Settings** -> **General** tab -> **API keys** section.
```
