---
title: "Cancel Subscription"
source_url: https://docs.rapyd.net/en/cancel-subscription.html
lang: en
---

# Cancel Subscription

Delete a subscription from the database.

When the subscription is canceled immediately, this method triggers the following webhooks:

- [Subscription Canceled Webhook](https://docs.rapyd.net/en/subscription-canceled-webhook.md "Subscription Canceled Webhook"). This webhook contains the same information as the response.
- [Subscription Updated Webhook](https://docs.rapyd.net/en/subscription-updated-webhook.md "Subscription Updated Webhook")
- [Invoice Created Webhook](https://docs.rapyd.net/en/invoice-created-webhook.md "Invoice Created Webhook")

The following asynchronous webhook provides information about later changes to the subscription object:

- [Invoice Payment Succeeded Webhook](https://docs.rapyd.net/en/invoice-payment-succeeded-webhook.md "Invoice Payment Succeeded Webhook")

When the subscription is canceled at the end of the current billing period, all of these webhooks are triggered asynchronously.

### Parameters

### Request Path Parameters

- - subscription
  - ID of the subscription. String starting with **sub_**.

### Request Query Parameters

- - cancel_at_period_end
  - Determines when the subscription is canceled.

    - **true** - Cancels the subscription at the end of the current period.
    - **false** - Cancels the subscription immediately.

    false

### Request Header Parameters

- - access_key
  - Unique access key provided by Rapyd for each authorized user.

    See [Developers](https://docs.rapyd.net/en/developers.md "Developers").
- - Content-Type
  - Indicates that the data appears in JSON format. Set to **application/json**.
- - salt
  - Random string. Recommended length: 8-16 characters.
- - signature
  - Signature calculated for each request individually.

    See [Request Signatures](https://docs.rapyd.net/en/request-signatures.md "Request Signatures").
- - timestamp
  - Timestamp for the request, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time") (seconds).

### Response Parameters

- - billing
  - Determines the method of billing at the end of the billing cycle. Set to **pay_automatically** - Rapyd generates a `payment` object, then attempts to pay it using the designated payment method.
- - billing_cycle_anchor
  - Determines the start of the next full billing cycle, as defined in the plan described in the `items`. One of the following values:

    - **now** - The present day. The next billing cycle starts right now.
    - *Timestamp* in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time") - A time in the future, at or after the end of the free trial period, not more than the length of the billing cycle. The current billing cycle will be shorter than all other billing cycles. Relevant to creating a subscription.
    - **unchanged** - The original billing cycle anchor is unchanged. Relevant to updating a subscription.
- - cancel_at_period_end
  - Determines the last date that charges accrue.

    - **true** - Charges accrue until the end of the current billing period, then the subscription is canceled.

      - When no trial period is set, after `cancel_at_period_end` is set to **true** the subscription will not be renewed at the next interval.
      - When a trial period is set after `cancel_at_period_end` is set to **true**, the subscription will not begin.
    - **false** - When the subscription is created, no end is defined. When [Cancel Subscription](https://docs.rapyd.net/en/cancel-subscription.md "Cancel Subscription") is run, charges stop immediately and the subscription is canceled.
- - cancel_checkout_url
  - URL where the customer is redirected after pressing **Back to Website** to exit the hosted page. This URL overrides the `merchant_website` URL. Does not support localhost URLs.
- - canceled_at
  - Date and time that the subscription is canceled by the customer or by the client. [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
- - complete_checkout_url
  - URL where the customer is redirected after pressing **Finish** to exit the hosted page. This URL overrides the `merchant_website` URL. Does not support localhost URLs.
- - complete_payment_url
  - URL where the customer is redirected when payment is successful, after returning from an external page such as a 3DS page. Does not support localhost URLs.
- - coupon
  - ID of a coupon to apply a discount to the subscription. String starting with **coupon_**.
- - created_at
  - Time of creation of this subscription, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
- - current_period_end
  - End of the current billing cycle, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
- - current_period_start
  - Start of the current billing cycle, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
- - customer_token
  - ID of the customer who pays for this subscription. String starting with **cus_**.
- - days_until_due
  - Number of days the customer has for paying this invoice.
- - discount
  - Contains information about the coupon that applies to the subscription. Returned as a **null** value when there is no discount.
- - ended_at
  - Date that the subscription was canceled or otherwise terminated. [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
- - error_payment_url
  - URL where the customer is redirected when payment is not successful, after returning from an external page, such as a 3DS page. Does not support localhost URLs.
- - id
  - ID of the Subscription object. String starting with **sub_**.
- - language
  - Determines the default language of the hosted page. For a list of values, see [List Supported Languages](https://docs.rapyd.net/en/list-supported-languages.md "List Supported Languages").

    - When this parameter is null, the language of the user's browser is used.
    - If the language of the user's browser cannot be determined, the default language is English.
- - merchant_reference_id
  - Identifier defined by the client for reference purposes. Limit: 45 characters.
- - metadata
  - A JSON object defined by the client. See [Metadata](https://docs.rapyd.net/en/metadata.md "Metadata").
- - page_expiration
  - End of the time when the customer can use the hosted page, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time"). If `page_expiration` is not set, the hosted page expires 14 days after creation.

    **Range:** 1 minute to 30 days.
- - payment_fields
  - Additional `payment_options` fields.
- - payment_method
  - Payment method used for paying invoices generated by this subscription. The ID is a string starting with **card_** or **other_**.
- - payout_fields
  - Reserved.
- - product
  - ID of a `product` object. String starting with **product_** Relevant to [List Subscriptions](https://docs.rapyd.net/en/list-subscriptions.md "List Subscriptions").
- - simultaneous_invoice
  - Determines when the system creates a payment for an invoice.

    - **true** - Payment is created when the invoice is issued.
    - **false** - Payment is created one hour after the invoice is issued.
- - status
  - Status of the subscription. One of the following:

    - **active** - The customer is currently paying for this subscription.
    - **canceled** - The customer has canceled this subscription, but it remains in the Rapyd database.
    - **past_due** - Payment for this subscription was not received by the end of the billing period.
    - **trialing** - The subscription is in its free trial period.
    - **unpaid** - An error occurred in the payment for this subscription.
- - subscription_items
  - An object or array of objects containing the following fields:

    - - data
      - Array of objects that describe individual subscription items.

        - - active
          - Indicates whether the subscription item is active.
        - - created
          - The time the subscription item was created, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
        - - id
          - ID of the subscription item. String starting with **subi_**.
        - - metadata
          - A JSON object defined by the client. See [Metadata](https://docs.rapyd.net/en/metadata.md "Metadata").
        - - plan
          - The pricing plan that defines the product and pricing structure associated with this subscription item. Cannot be a plan that is already part of the subscription. For details of the fields in the 'plan' object, see [Create Plan](https://docs.rapyd.net/en/create-plan.md "Create Plan").
        - - quantity
          - The number of units of the service defined in the plan. Integer. This number can be updated during the billing cycle using [Update Subscription](https://docs.rapyd.net/en/update-subscription.md "Update Subscription") or [Update Subscription Item](https://docs.rapyd.net/en/update-subscription-item.md "Update Subscription Item").
        - - subscription_id
          - ID of the subscription that this subscription item belongs to. String starting with **sub_**.
    - - has_more
      - Indicates that the number of `subscription_items` records is greater than the number of the returned records.
    - - total_count
      - Number of `subscription_items` records.
    - - url
      - URL for the record of all subscription items for this subscription.
- - tax_percent
  - The percentage tax rate that is applied to the subtotal of the invoice, after subtracting all discounts. Decimal.
- - trial_end
  - The date and time of the end of the customer's free trial period, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time"), or the string **now**. Relevant when `trial_period_days` is not defined.
- - trial_from_plan
  - Determines whether the subscription uses the free trial period defined in the plan.

    - **true** - Uses the trial period defined in the plans added to the subscription. When `trial_from_plan` is true, all plans in the subscription must have the same value for `trial_period_days`.
    - **false** - Uses the trial period defined in the subscription.
- - trial_period_days
  - The number of days in the customer's free trial period. Integer. Range: 0-730.

    Relevant when `trial_end` is not defined.
- - trial_start
  - Date and time of the start of the customer's free trial period, in [Unix time](https://docs.rapyd.net/en/glossary.md#UUID-945d98cf-adae-e1cf-2606-c7fae8b4a7e1_unix_time "Unix time").
- - type
  - The type of the subscription.

    - **payment**

### Code Samples

- - .NET

    - ```csharp
      using System;
      using System.Text.Json;

      namespace RapydApiRequestSample
      {
          class Program
          {
              static void Main(string[] args)
              {
                  try
                  {

                      string subscriptionItem = "sub_5b47720e0281b501092a695773fb2279";

                      string result = RapydApiRequestSample.Utilities.MakeRequest("DELETE", $"/v1/payments/subscriptions/{subscriptionItem}");

                      Console.WriteLine(result);
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("Error completing request: " + e.Message);
                  }
              }
          }
      }
      ```
- - JavaScript

    - ```javascript
      const makeRequest = require('<path-to-your-utility-file>/utilities').makeRequest;

      async function main() {
        try {
          const result = await makeRequest(
            'DELETE',
            '/v1/payments/subscriptions/sub_5b47720e0281b501092a695773fb2279'
          );

          console.log(result);
        } catch (error) {
          console.error('Error completing request', error);
        }
      }
      ```
- - PHP

    - ```php
      <?php
      $path = $_SERVER['DOCUMENT_ROOT'];
      $path .= "/<path-to-your-utility-file>/utilities.php";
      include($path);
      try {
          $object = make_request('delete', '/v1/payments/subscriptions/sub_5b47720e0281b501092a695773fb2279');
          var_dump($object);
      } catch(Exception $e) {
          echo "Error: $e";
      }
      ?>
      ```
- - Python

    - ```python
      from pprint import pprint

      from utilities import make_request

      result = make_request(method='delete', path='/v1/payments/subscriptions/sub_c120969b649d4a029b660228e00c8d97')
      pprint(result)
      ```

- /v1/payments/subscriptions/:subscription

- Cancel Subscription Immediately
- ```curl
  curl -X delete 'https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_6f634b7aced3e9f872f66fc8ef708ae5' \
  -H 'access_key: your-access-key-here' \
  -H 'Content-Type: application/json' \
  -H 'salt: your-random-string-here' \
  -H 'signature: your-calculated-signature-here' \
  -H 'timestamp: your-unix-timestamp-here'
  ```
- ```json
  {
      "status": {
          "error_code": "",
          "status": "SUCCESS",
          "message": "",
          "response_code": "",
          "operation_id": "1fc01453-f7ee-4535-9bb8-562493dd3df7"
      },
      "data": {
          "id": "sub_5f599d4bec8598f8fcee195df58b1621",
          "billing": "pay_automatically",
          "billing_cycle_anchor": 1761226148,
          "created_at": 1761226148,
          "customer_token": "cus_4e25112ac20e144ad073a614dc46934b",
          "days_until_due": 30,
          "metadata": {},
          "tax_percent": 0,
          "cancel_at_period_end": false,
          "canceled_at": 1761226167,
          "current_period_end": 1761226167,
          "current_period_start": 1761226148,
          "discount": null,
          "ended_at": null,
          "subscription_items": {
              "data": [
                  {
                      "id": "subi_372c7bcee656bc2123697f53ebc474f8",
                      "created": 1761226148,
                      "metadata": null,
                      "quantity": 1,
                      "plan": {
                          "id": "plan_9338044c6ca654abd79da7319c40b7dc",
                          "aggregate_usage": "sum",
                          "amount": 0,
                          "billing_scheme": "tiered",
                          "created_at": 1761210135,
                          "currency": "USD",
                          "interval": "month",
                          "interval_count": 2,
                          "metadata": {},
                          "product": {
                              "id": "product_88fde8f1365082b50e8f4b37127edd99",
                              "active": true,
                              "attributes": [
                                  "users"
                              ],
                              "created_at": 1673509005,
                              "description": "SaaS Users",
                              "images": [
                                  "64bit-encoded-image-1"
                              ],
                              "metadata": {
                                  "merchant_defined": true
                              },
                              "name": "SaaS Users",
                              "package_dimensions": {
                                  "height": 0,
                                  "length": 0,
                                  "weight": 0,
                                  "width": 0
                              },
                              "shippable": false,
                              "skus": [],
                              "statement_descriptor": "Number of Users",
                              "type": "services",
                              "unit_label": "users",
                              "updated_at": 1673509005
                          },
                          "nickname": "SaaS Users",
                          "tiers": [
                              {
                                  "amount": 35,
                                  "up_to": 5,
                                  "flat_amount": 25
                              },
                              {
                                  "amount": 30,
                                  "up_to": 10,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 25,
                                  "up_to": 25,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 20,
                                  "up_to": 100,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 15,
                                  "up_to": 500,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 10,
                                  "up_to": "inf",
                                  "flat_amount": 0
                              }
                          ],
                          "tiers_mode": "volume",
                          "transform_usage": {
                              "divide_by": 1,
                              "round": "up"
                          },
                          "trial_period_days": 0,
                          "usage_type": "licensed",
                          "active": true
                      }
                  }
              ],
              "has_more": false,
              "total_count": 1,
              "url": "/v1/subscription_items?subscription=sub_5f599d4bec8598f8fcee195df58b1621"
          },
          "status": "canceled",
          "trial_end": null,
          "trial_start": null,
          "payment_method": "card_b9548d9b53a565b2315fecdc6c87f158",
          "payment_fields": null,
          "payout_fields": null,
          "type": "payment",
          "simultaneous_invoice": true
      }
  }
  ```

- Cancel Subscription at end of billing cycle
- ```curl
  curl -X delete 'https://sandboxapi.rapyd.net/v1/payments/subscriptions/sub_6f634b7aced3e9f872f66fc8ef708ae5?cancel_at_period_end=true' \
  -H 'access_key: your-access-key-here' \
  -H 'Content-Type: application/json' \
  -H 'salt: your-random-string-here' \
  -H 'signature: your-calculated-signature-here' \
  -H 'timestamp: your-unix-timestamp-here'
  ```
- ```json
  {
      "status": {
          "error_code": "",
          "status": "SUCCESS",
          "message": "",
          "response_code": "",
          "operation_id": "167bbb6c-d540-4bac-b8b1-f188df0e11ed"
      },
      "data": {
          "id": "sub_d25185e18bd1509108cbc0b285a8243c",
          "billing": "pay_automatically",
          "billing_cycle_anchor": 1761226223,
          "created_at": 1761226223,
          "customer_token": "cus_4e25112ac20e144ad073a614dc46934b",
          "days_until_due": 30,
          "metadata": {},
          "tax_percent": 0,
          "cancel_at_period_end": true,
          "canceled_at": 1761226268,
          "current_period_end": 1766496623,
          "current_period_start": 1761226223,
          "discount": null,
          "ended_at": null,
          "subscription_items": {
              "data": [
                  {
                      "id": "subi_1bba618bdb03542772fd608e3e6fe2bf",
                      "created": 1761226223,
                      "metadata": null,
                      "quantity": 1,
                      "plan": {
                          "id": "plan_9338044c6ca654abd79da7319c40b7dc",
                          "aggregate_usage": "sum",
                          "amount": 0,
                          "billing_scheme": "tiered",
                          "created_at": 1761210135,
                          "currency": "USD",
                          "interval": "month",
                          "interval_count": 2,
                          "metadata": {},
                          "product": {
                              "id": "product_88fde8f1365082b50e8f4b37127edd99",
                              "active": true,
                              "attributes": [
                                  "users"
                              ],
                              "created_at": 1673509005,
                              "description": "SaaS Users",
                              "images": [
                                  "64bit-encoded-image-1"
                              ],
                              "metadata": {
                                  "merchant_defined": true
                              },
                              "name": "SaaS Users",
                              "package_dimensions": {
                                  "height": 0,
                                  "length": 0,
                                  "weight": 0,
                                  "width": 0
                              },
                              "shippable": false,
                              "skus": [],
                              "statement_descriptor": "Number of Users",
                              "type": "services",
                              "unit_label": "users",
                              "updated_at": 1673509005
                          },
                          "nickname": "SaaS Users",
                          "tiers": [
                              {
                                  "amount": 35,
                                  "up_to": 5,
                                  "flat_amount": 25
                              },
                              {
                                  "amount": 30,
                                  "up_to": 10,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 25,
                                  "up_to": 25,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 20,
                                  "up_to": 100,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 15,
                                  "up_to": 500,
                                  "flat_amount": 0
                              },
                              {
                                  "amount": 10,
                                  "up_to": "inf",
                                  "flat_amount": 0
                              }
                          ],
                          "tiers_mode": "volume",
                          "transform_usage": {
                              "divide_by": 1,
                              "round": "up"
                          },
                          "trial_period_days": 0,
                          "usage_type": "licensed",
                          "active": true
                      }
                  }
              ],
              "has_more": false,
              "total_count": 1,
              "url": "/v1/subscription_items?subscription=sub_d25185e18bd1509108cbc0b285a8243c"
          },
          "status": "active",
          "trial_end": null,
          "trial_start": null,
          "payment_method": "card_b9548d9b53a565b2315fecdc6c87f158",
          "payment_fields": null,
          "payout_fields": null,
          "type": "payment",
          "simultaneous_invoice": true
      }
  }
  ```
