Skip to main content

Documentation

Create Subscription

Create a subscription for regular, automatic payments.

This method triggers the Customer Subscription Created webhook. This webhook contains the same information as the response.

The following asynchronous webhooks provide information about later changes to the subscription:

Depending on the parameters in the request, some of these webhooks are triggered synchronously.

Note

  • Clients with PCI certification can handle personal identifying information for cards. Clients that are not PCI-certified can use Checkout Page or Payment Link to collect money from a card.

  • Before you create a subscription with a payment method token, verify that the value of is_tokenizable is true. See also Payment Method Type.

  • If the payment method category is card, certain operations require 3DS authentication. To simulate this authentication in the sandbox, use the URL from the redirect_url field that appears in Invoice Payment Created Webhook and Payment Succeeded Webhook. See Simulating 3DS Authentication.

  • A card payment that requires 3DS authentication must be authenticated within 15 minutes.

  • All plans in the subscription must have the same value for trial_period_days.

Code Samples
    • .NET

      • using System;
        using System.Text.Json;
        
        namespace RapydApiRequestSample
        {
            class Program
            {
                static void Main(string[] args)
                {
                    try
                    {
                        var requestObj = new
                        {
                            customer = "cus_ec543c4dfcf6ac1d015b2e3e4fd07180",
                            billing = "pay_automatically",
                            billing_cycle_anchor = "",
                            cancel_at_period_end = true,
                            coupon = "",
                            days_until_due = string.Empty,
                            payment_method = "card_156da7de485275ad0bae798b7cb8fea1",
                            subscription_items = new Object[] {
                                new {
                                    plan = "plan_51788cf4ed1b672cb0a2a97773887f5b",
                                    quantity = 1
                                }
                            },
                            metadata = new
                            {
                                merchant_defined = true
                            },
                            tax_percent = 10.5,
                            trial_end = 0,
                            trial_from_plan = true,
                            trial_period_days = string.Empty,
                            plan_token = ""
                        };
        
                        string request = JsonSerializer.Serialize(requestObj);
        
                        string result = RapydApiRequestSample.Utilities.MakeRequest("POST", "/v1/payments/subscriptions", request);
            
                        Console.WriteLine(result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error completing request: " + e.Message);
                    }
                }
            }
        }
        
    • JavaScript

      • const makeRequest = require('<path-to-your-utility-file>/utilities').makeRequest;
        
        async function main() {
          try {
            const body = {
              customer: 'cus_ec543c4dfcf6ac1d015b2e3e4fd07180',
              billing: 'pay_automatically',
              billing_cycle_anchor: '',
              cancel_at_period_end: true,
              coupon: '',
              days_until_due: null,
              payment_method: 'card_156da7de485275ad0bae798b7cb8fea1',
              subscription_items: [
                {
                  plan: 'plan_51788cf4ed1b672cb0a2a97773887f5b',
                  quantity: 1
                }
              ],
              metadata: {
                merchant_defined: true
              },
              tax_percent: 10.5,
              trial_end: 0,
              trial_from_plan: true,
              trial_period_days: null,
              plan_token: ''
            };
            const result = await makeRequest('POST', '/v1/payments/subscriptions', body);
        
            console.log(result);
          } catch (error) {
            console.error('Error completing request', error);
          }
        }
        
    • PHP

      • <?php
        $path = $_SERVER['DOCUMENT_ROOT'];
        $path .= "/<path-to-your-utility-file>/utilities.php";
        include($path);
        
        try {
            $body = [
                "customer" => "cus_ec543c4dfcf6ac1d015b2e3e4fd07180",
                "billing" => "pay_automatically",
                "billing_cycle_anchor" => "",
                "cancel_at_period_end" => true,
                "coupon" => "",
                "days_until_due" => null,
                "payment_method" => "card_156da7de485275ad0bae798b7cb8fea1",
                "subscription_items" => [
                    array(
                        "plan" => "plan_51788cf4ed1b672cb0a2a97773887f5b",
                        "quantity" => 1
                    )
                ],
                "metadata" => array(
                    "merchant_defined" => true
                ),
                "tax_percent" => 10.5,
                "trial_end" => 0,
                "trial_from_plan" => true,
                "trial_period_days" => null,
                "plan_token" => ""
            ];
            $object = make_request('post', '/v1/payments/subscriptions', $body);
            var_dump($object);
        } catch(Exception $e) {
            echo "Error: $e";
        }
        ?>
    • Python

      • from pprint import pprint
        
        from utilities import make_request
        
        subscription = {
            "customer": "cus_ec543c4dfcf6ac1d015b2e3e4fd07180",
            "billing": "pay_automatically",
            "billing_cycle_anchor": "",
            "cancel_at_period_end": True,
            "coupon": "",
            "days_until_due": "",
            "payment_method": "card_156da7de485275ad0bae798b7cb8fea1",
            "subscription_items": [
                {
                    "plan": "plan_51788cf4ed1b672cb0a2a97773887f5b",
                    "quantity": 1
                }
            ],
            "metadata": {
                "merchant_defined": True
            },
            "tax_percent": 10.5,
            "trial_end": 0,
            "trial_from_plan": True,
            "trial_period_days": "",
            "plan_token": "",
        }
        
        result = make_request(method='post', path='/v1/subscriptions', body=subscription)
        pprint(result)
        
        
  • /v1/payments/subscriptions

  • Create Subscription

  • curl -X post 'https://sandboxapi.rapyd.net/v1/payments/subscriptions' \
    -H 'access_key: your-access-key-here' \
    -H 'Content-Type: application/json' \
    -H 'idempotency: your-idempotency-parameter-here' \
    -H 'salt: your-random-string-here' \
    -H 'signature: your-calculated-signature-here' \
    -H 'timestamp: your-unix-timestamp-here' \
    --data-raw '{
        "customer": "cus_85579bd2098d5baf407f7f08313f5318",
        "billing": "pay_automatically",
        "subscription_items": [
            {
                "plan": "plan_9338044c6ca654abd79da7319c40b7dc",
                "quantity": 1
            }
        ]
    }'
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "e2fad0b3-c7f8-443c-ac95-d26a2f8f8f1c"
        },
        "data": {
            "id": "sub_1219e2329c4c5b26f08203d826a59d92",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1761213125,
            "created_at": 1761213125,
            "customer_token": "cus_85579bd2098d5baf407f7f08313f5318",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1766483525,
            "current_period_start": 1761213125,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_353e05ee1107b6ecb9f1a7661358e575",
                        "created": 1761213125,
                        "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"
                            },
                            "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": 0,
                "url": "/v1/subscription_items?subscription=sub_1219e2329c4c5b26f08203d826a59d92"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_169329a41beed1e8fedb9e9bbc10d6b6",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true
        }
    }
  • Create Subscription with Payment Fields

  • curl -X post  'https://sandboxapi.rapyd.net/v1/payments/subscriptions' \
    -H 'access_key: your-access-key-here' \
    -H 'Content-Type: application/json' \
    -H 'idempotency: your-idempotency-parameter-here' \
    -H 'salt: your-random-string-here' \
    -H 'signature: your-calculated-signature-here' \
    -H 'timestamp: your-unix-timestamp-here' \
    --data-raw '{
        "customer": "cus_85579bd2098d5baf407f7f08313f5318",
        "billing": "pay_automatically",
        "cancel_at_period_end": false,
        "coupon": "",
        "payment_method": {
            "type": "gb_visa_card",
            "fields": {
                "name": "John Doe",
                "number": "4111111111111111",
                "expiration_month": "12",
                "expiration_year": "30",
                "cvv": "111"
            }
        },
        "subscription_items": [
            {
                "plan": "plan_d7dcedc6ec79b21d3fe8d5fde1af47f7",
                "quantity": 1
            }
        ],
        "tax_percent": 10.5,
        "trial_end": 0,
        "trial_from_plan": true,
        "trial_period_days": "",
        "payment_fields": {
            "error_payment_url": "https://error.example1234.net",
            "complete_payment_url": "https://complete.example5678.net"
        }
    }'
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "afb456ac-9af0-4de3-a81b-1a2b192de76c"
        },
        "data": {
            "id": "sub_1c9334696bc18fd89681cc800f758040",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1761818252,
            "created_at": 1761213452,
            "customer_token": "cus_85579bd2098d5baf407f7f08313f5318",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 10.5,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1761818252,
            "current_period_start": 1761213452,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_512e267b9d6eb3bb9fe5c8c5a851c1a2",
                        "created": 1761213452,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_d7dcedc6ec79b21d3fe8d5fde1af47f7",
                            "aggregate_usage": "sum",
                            "amount": 9.99,
                            "billing_scheme": "per_unit",
                            "created_at": 1761210540,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {},
                            "product": {
                                "id": "product_4ba001c7655c4a0ee4f471dbaccaa305"
                            },
                            "nickname": "Unlimited Plan",
                            "tiers": [],
                            "tiers_mode": "",
                            "transform_usage": {
                                "divide_by": 1,
                                "round": "up"
                            },
                            "trial_period_days": 7,
                            "usage_type": "licensed",
                            "active": true
                        }
                    }
                ],
                "has_more": false,
                "total_count": 0,
                "url": "/v1/subscription_items?subscription=sub_1c9334696bc18fd89681cc800f758040"
            },
            "status": "trialing",
            "trial_end": 1761818252,
            "trial_start": 1761213452,
            "payment_method": "card_f7541a823319e86d3e628894fa6261c1",
            "payment_fields": {
                "error_payment_url": "https://error.example1234.net",
                "complete_payment_url": "https://complete.example5678.net"
            },
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true
        }
    }
  • Create Subscription with Multiple Items

  • curl -X post 'https://sandboxapi.rapyd.net/v1/payments/subscriptions' \
    -H 'access_key: your-access-key-here' \
    -H 'Content-Type: application/json' \
    -H 'idempotency: your-idempotency-parameter-here' \
    -H 'salt: your-random-string-here' \
    -H 'signature: your-calculated-signature-here' \
    -H 'timestamp: your-unix-timestamp-here' \
    --data-raw '{
        "customer": "cus_4e25112ac20e144ad073a614dc46934b",
        "billing": "pay_automatically",
        "cancel_at_period_end": false,
        "payment_method": "card_b9548d9b53a565b2315fecdc6c87f158",
        "subscription_items": [
            {
                "plan": "plan_4304ce1f6d347a2204711cd2d5354fa9",
                "quantity": 1
            },
            {
                "plan": "plan_9338044c6ca654abd79da7319c40b7dc",
                "quantity": 1
            }
        ]
    }'
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "b1934108-fb91-4cc1-8a73-5a0a7eb8b1a0"
        },
        "data": {
            "id": "sub_a16955cdb7e1a0b52395ff1e29f8ece7",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1761213828,
            "created_at": 1761213828,
            "customer_token": "cus_4e25112ac20e144ad073a614dc46934b",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1766484228,
            "current_period_start": 1761213828,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_117a8e763b2f9965329a2081d5ded5e7",
                        "created": 1761213828,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_4304ce1f6d347a2204711cd2d5354fa9",
                            "aggregate_usage": "sum",
                            "amount": 1500,
                            "billing_scheme": "per_unit",
                            "created_at": 1761210402,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 2,
                            "metadata": {},
                            "product": {
                                "id": "product_88fde8f1365082b50e8f4b37127edd99"
                            },
                            "nickname": "Licenses",
                            "tiers": [],
                            "tiers_mode": "",
                            "transform_usage": {
                                "divide_by": 5,
                                "round": "up"
                            },
                            "trial_period_days": 0,
                            "usage_type": "licensed",
                            "active": true
                        }
                    },
                    {
                        "id": "subi_42a0955d0e5542c2b7f6350a9948c73e",
                        "created": 1761213828,
                        "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"
                            },
                            "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": 0,
                "url": "/v1/subscription_items?subscription=sub_a16955cdb7e1a0b52395ff1e29f8ece7"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_b9548d9b53a565b2315fecdc6c87f158",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": true
        }
    }