> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stablepayfi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Cancel subscription

> Cancel a subscription immediately, or schedule it to cancel at the end of the current billing period.



## OpenAPI

````yaml /en/api/subscription-openapi.json post /api/v1/subscriptions/{subscription_id}/cancel
openapi: 3.1.0
info:
  title: StablePay Subscription API
  version: '2026-04-20'
  summary: >-
    StablePay subscription endpoints: subscriptions, payment sessions, payment
    methods, refunds, and webhook events.
  description: >-
    OpenAPI specification for the StablePay subscription service.


    ## Basics

    - Base URL: `https://api.stablepay.co`

    - Prefix: `/api/v1`

    - Protocol: HTTPS

    - Format: JSON (UTF-8)


    ## Authentication

    All endpoints require `Authorization: Bearer {api_key}`. Write operations
    should also include replay-protection headers: `X-StablePay-Timestamp`,
    `X-StablePay-Nonce`, `X-StablePay-Signature`.


    ## Amount precision

    Amount fields are expressed in the smallest unit of the currency (e.g.
    `1999` represents $19.99 for USD; USDT/USDC use 6 decimal places).
servers:
  - url: https://api.stablepay.co
    description: Production
security:
  - bearerAuth: []
    stablepayTimestamp: []
    stablepayNonce: []
    stablepaySignature: []
tags:
  - name: Subscriptions
    description: Create, retrieve, list, and cancel subscriptions.
paths:
  /api/v1/subscriptions/{subscription_id}/cancel:
    post:
      tags:
        - Subscriptions
      summary: Cancel subscription
      description: >-
        Cancel a subscription immediately, or schedule it to cancel at the end
        of the current billing period.
      operationId: cancelSubscription
      parameters:
        - $ref: '#/components/parameters/SubscriptionId'
        - $ref: '#/components/parameters/XStablePayTimestamp'
        - $ref: '#/components/parameters/XStablePayNonce'
        - $ref: '#/components/parameters/XStablePaySignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CancelSubscriptionRequest'
      responses:
        '200':
          description: Canceled successfully.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Subscription'
        '400':
          description: Invalid request parameters.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Authentication failed.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Subscription not found or not accessible.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Server error.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  parameters:
    SubscriptionId:
      name: subscription_id
      in: path
      required: true
      description: Subscription ID.
      schema:
        type: string
    XStablePayTimestamp:
      name: X-StablePay-Timestamp
      in: header
      required: true
      description: Unix timestamp in seconds. Must be within 5 minutes of server time.
      schema:
        type: string
      example: '1735689600'
    XStablePayNonce:
      name: X-StablePay-Nonce
      in: header
      required: true
      description: Replay-protection nonce, 16-64 characters. Use a fresh UUID per request.
      schema:
        type: string
        minLength: 16
        maxLength: 64
      example: 550e8400-e29b-41d4-a716-446655440000
    XStablePaySignature:
      name: X-StablePay-Signature
      in: header
      required: true
      description: >-
        Lowercase hex HMAC-SHA256 of `{timestamp}.{nonce}.{requestBody}` signed
        with the merchant API Secret. For GET requests, `requestBody` is an
        empty string.
      schema:
        type: string
      example: a1b2c3d4e5f6...
  schemas:
    CancelSubscriptionRequest:
      type: object
      properties:
        cancel_at_period_end:
          type: boolean
          default: false
          description: >-
            Whether to cancel at the end of the current billing period. Defaults
            to false for immediate cancellation.
        cancellation_reason:
          type: string
          description: Cancellation reason.
        cancellation_comment:
          type: string
          description: Cancellation comment.
    Subscription:
      type: object
      description: Subscription object.
      properties:
        id:
          type: string
          description: Subscription ID.
        object:
          type: string
          example: subscription
        customer:
          type: string
          description: External customer ID.
        store_id:
          type: string
          description: Store ID.
        currency:
          type: string
          description: Currency.
        description:
          type: string
          description: Subscription description.
        status:
          $ref: '#/components/schemas/SubscriptionStatus'
        items:
          type: array
          items:
            $ref: '#/components/schemas/SubscriptionItem'
          description: Subscription items.
        payment_method_id:
          type: string
          description: Default payment method ID.
        billing_cycle_anchor:
          type: integer
          format: int64
          description: Billing cycle anchor, Unix seconds.
        current_period_start:
          type: integer
          format: int64
          description: Current period start, Unix seconds.
        current_period_end:
          type: integer
          format: int64
          description: Current period end, Unix seconds.
        canceled_at:
          type: integer
          format: int64
          description: Cancellation time, Unix seconds.
        cancel_at:
          type: integer
          format: int64
          description: Scheduled cancellation time, Unix seconds.
        latest_invoice:
          type: string
          description: Latest invoice ID.
        latest_invoice_object:
          allOf:
            - $ref: '#/components/schemas/Invoice'
          description: Full invoice object returned when `expand=latest_invoice`.
        customer_object:
          allOf:
            - $ref: '#/components/schemas/Customer'
          description: Full customer object returned when `expand=customer`.
        payment_method_object:
          allOf:
            - $ref: '#/components/schemas/PaymentMethod'
          description: Full payment method object returned when `expand=payment_method`.
        checkout_url:
          type: string
          description: Initial-payment checkout URL, returned on creation.
        metadata:
          type: object
          additionalProperties: true
          description: Merchant metadata.
        created:
          type: integer
          format: int64
          description: Creation time, Unix seconds.
    ErrorResponse:
      type: object
      description: Error response.
      properties:
        error:
          type: object
          properties:
            type:
              type: string
              description: Error type.
            code:
              type: string
              description: Error code.
            message:
              type: string
              description: Error message.
            param:
              type: string
              description: Related parameter.
            doc_url:
              type: string
              description: Error documentation URL.
            decline_code:
              type: string
              description: Decline code.
            request_log_url:
              type: string
              description: Request log URL.
            charge:
              type: string
              description: Related payment ID.
        request_id:
          type: string
          description: Request ID.
        timestamp:
          type: integer
          format: int64
          description: Error timestamp in Unix seconds.
    SubscriptionStatus:
      type: string
      enum:
        - incomplete
        - trialing
        - active
        - past_due
        - canceled
        - incomplete_expired
      description: Subscription status.
    SubscriptionItem:
      type: object
      properties:
        id:
          type: string
          description: Subscription item ID.
        price_data:
          $ref: '#/components/schemas/PriceData'
        quantity:
          type: integer
          minimum: 1
          description: Quantity.
        metadata:
          type: object
          additionalProperties: true
          description: Subscription item metadata.
    Invoice:
      type: object
      required:
        - id
        - object
        - subscription_id
        - customer
        - amount_due
        - amount_paid
        - amount_remaining
        - currency
        - status
        - payment_status
        - billing_reason
        - period_start
        - period_end
        - created
      properties:
        id:
          type: string
          description: Invoice ID.
        object:
          type: string
          example: invoice
        subscription_id:
          type: string
          description: Related subscription ID.
        customer:
          type: string
          description: Customer ID.
        amount_due:
          type: integer
          format: int64
          description: Amount due in minor units.
        amount_paid:
          type: integer
          format: int64
          description: Amount paid in minor units.
        amount_remaining:
          type: integer
          format: int64
          description: Amount remaining in minor units.
        currency:
          type: string
          description: Currency.
        status:
          $ref: '#/components/schemas/InvoiceStatus'
        payment_status:
          type: string
          enum:
            - pending
            - processing
            - completed
            - failed
            - canceled
            - expired
          description: Invoice payment status.
        billing_reason:
          type: string
          description: >-
            Billing reason, for example `subscription_create` or
            `subscription_cycle`.
        frequency:
          type: string
          description: Human-readable billing frequency, for example `Monthly`.
        recurring_interval:
          type: string
          enum:
            - day
            - week
            - month
            - year
          description: Recurring interval.
        recurring_interval_count:
          type: integer
          format: int32
          description: Recurring interval count.
        period_start:
          type: integer
          format: int64
          description: Billing period start, Unix seconds.
        period_end:
          type: integer
          format: int64
          description: Billing period end, Unix seconds.
        payment_id:
          type: string
          description: >-
            Related payment session ID. The API prefers payment_session_id and
            falls back to trade_order_id.
        paid_at:
          type: integer
          format: int64
          description: Payment completion time, Unix seconds.
        due_date:
          type: integer
          format: int64
          description: Due date, Unix seconds.
        created:
          type: integer
          format: int64
          description: Creation time, Unix seconds.
    Customer:
      type: object
      properties:
        id:
          type: string
          description: Customer ID.
        object:
          type: string
          example: customer
        merchant_id:
          type: string
          description: Merchant ID.
        external_customer_id:
          type: string
          description: External customer ID.
        email:
          type: string
          format: email
          description: Customer email.
        name:
          type: string
          description: Customer name.
        phone:
          type: string
          description: Customer phone number.
        default_payment_method:
          type: string
          description: Default payment method ID.
        description:
          type: string
          description: Customer description.
        created:
          type: integer
          format: int64
          description: Creation time, Unix seconds.
    PaymentMethod:
      type: object
      properties:
        id:
          type: string
          description: Payment method ID.
        object:
          type: string
          example: payment_method
        customer:
          type: string
          description: Customer ID.
        merchant_id:
          type: string
          description: Merchant ID.
        type:
          type: string
          description: Payment method type.
        currency:
          type: string
          description: Currency.
        chain_type:
          type: integer
          format: int32
          description: Chain type.
        status:
          type: string
          enum:
            - active
            - inactive
            - revoked
            - unknown
          description: Payment method status.
        mandate_id:
          type: string
          description: Mandate ID.
        metadata:
          type: object
          additionalProperties: true
          description: Metadata.
        created:
          type: integer
          format: int64
          description: Creation time, Unix seconds.
    PriceData:
      type: object
      properties:
        id:
          type: string
          description: >-
            Price ID. This field is usually not returned by the current HTTP
            response.
        currency:
          type: string
          description: Currency.
        product:
          type: string
          description: Product ID or product name.
        unit_amount:
          type: integer
          format: int64
          description: Unit amount in minor units.
        recurring:
          $ref: '#/components/schemas/RecurringConfig'
    InvoiceStatus:
      type: string
      enum:
        - open
        - paid
        - void
      description: Invoice status.
    RecurringConfig:
      type: object
      properties:
        interval:
          type: string
          enum:
            - day
            - week
            - month
            - year
          description: Billing interval.
        interval_count:
          type: integer
          description: Billing interval count. Currently fixed to 1 by the server.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API Key
      description: Bearer API Key, for example `Bearer sk_live_xxx`.
    stablepayTimestamp:
      type: apiKey
      in: header
      name: X-StablePay-Timestamp
      description: Unix timestamp in seconds. Must be within 5 minutes of server time.
    stablepayNonce:
      type: apiKey
      in: header
      name: X-StablePay-Nonce
      description: Replay-protection nonce, 16-64 characters. Use a fresh UUID per request.
    stablepaySignature:
      type: apiKey
      in: header
      name: X-StablePay-Signature
      description: >-
        Lowercase hex HMAC-SHA256 of `{timestamp}.{nonce}.{requestBody}` signed
        with the merchant API Secret. For GET requests, `requestBody` is an
        empty string.

````