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'
    -d '{
        "customer": "cus_b306773fc73d23e5a9b4347984606cf7",
        "billing": "pay_automatically",
        "cancel_at_period_end": true,
        "coupon": "",
        "payment_method": "card_3c000ae5db06762a2c8a00ca514fa336",
        "subscription_items": [
            {
                "plan": "plan_aa8c4248e0f2a8cdc5f2139a8bf38f10",
                "quantity": 1
            }
        ],
        "metadata": {
            "merchant_defined": true
        },
        "tax_percent": 10.5,
        "trial_end": 0,
        "trial_from_plan": true,
        "trial_period_days": null,
        "proration_date":"1699194033"
    }
    '
    
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "af5d9b80-9d3f-4f2c-9594-5904787ea7fd"
        },
        "data": {
            "id": "sub_e434b29193daa54d384da87c4ef03c46",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1698589289,
            "created_at": 1698589289,
            "customer_token": "cus_b306773fc73d23e5a9b4347984606cf7",
            "days_until_due": 30,
            "metadata": {
                "merchant_defined": true
            },
            "tax_percent": 10.5,
            "cancel_at_period_end": true,
            "canceled_at": null,
            "current_period_end": 1701267689,
            "current_period_start": 1698589289,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_327bf79d360433c6fc693285ff994d76",
                        "created": 1698589289,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_aa8c4248e0f2a8cdc5f2139a8bf38f10",
                            "aggregate_usage": "sum",
                            "amount": 0,
                            "billing_scheme": "tiered",
                            "created_at": 1698588009,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {},
                            "product": "product_af760dadef7c1d4106e2558bcf6799f9",
                            "nickname": "Transit Use",
                            "tiers": [
                                {
                                    "amount": 4,
                                    "up_to": 5,
                                    "flat_amount": 1
                                },
                                {
                                    "amount": 3,
                                    "up_to": 10,
                                    "flat_amount": 0
                                },
                                {
                                    "amount": 2,
                                    "up_to": 20,
                                    "flat_amount": 0
                                },
                                {
                                    "amount": 1,
                                    "up_to": "inf",
                                    "flat_amount": 0
                                }
                            ],
                            "tiers_mode": "graduated",
                            "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_e434b29193daa54d384da87c4ef03c46"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_3c000ae5db06762a2c8a00ca514fa336",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": false
        }
    }
  • 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'
    -d '{
        "customer": "cus_b306773fc73d23e5a9b4347984606cf7",
        "billing": "pay_automatically",
        "cancel_at_period_end": false,
        "coupon": "",
        "payment_method": {
            "type": "gb_visa_card",
            "fields": {
                "name": "John Doe",
                "number": "4111111111111111",
                "expiration_month": "05",
                "expiration_year": "25",
                "cvv": "111"
            }
        },
        "subscription_items": [
            {
                "plan": "plan_aa8c4248e0f2a8cdc5f2139a8bf38f10",
                "quantity": 1
            }
        ],
        "metadata": {
            "merchant_defined": "true"
        },
        "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": "bc404739-8ce9-4d4d-8192-2cc6eca89ddb"
        },
        "data": {
            "id": "sub_aa388923118ab61a65dd73bdfa2bd3cd",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1700138214,
            "created_at": 1700138214,
            "customer_token": "cus_b306773fc73d23e5a9b4347984606cf7",
            "days_until_due": 30,
            "metadata": {
                "merchant_defined": "true"
            },
            "tax_percent": 10.5,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1702730214,
            "current_period_start": 1700138214,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_295deb5d5052a237ecaee9663ea0365d",
                        "created": 1700138214,
                        "metadata": null,
                        "quantity": 1,
                        "plan": {
                            "id": "plan_aa8c4248e0f2a8cdc5f2139a8bf38f10",
                            "aggregate_usage": "sum",
                            "amount": 0,
                            "billing_scheme": "tiered",
                            "created_at": 1698588009,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {},
                            "product": "product_af760dadef7c1d4106e2558bcf6799f9",
                            "nickname": "Transit Use",
                            "tiers": [
                                {
                                    "amount": 4,
                                    "up_to": 5,
                                    "flat_amount": 1
                                },
                                {
                                    "amount": 3,
                                    "up_to": 10,
                                    "flat_amount": 0
                                },
                                {
                                    "amount": 2,
                                    "up_to": 20,
                                    "flat_amount": 0
                                },
                                {
                                    "amount": 1,
                                    "up_to": "inf",
                                    "flat_amount": 0
                                }
                            ],
                            "tiers_mode": "graduated",
                            "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_aa388923118ab61a65dd73bdfa2bd3cd"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_a340e3bb4ea898f4d00aff7f454141be",
            "payment_fields": {
                "error_payment_url": "https://error.example1234.net",
                "complete_payment_url": "https://complete.example5678.net"
            },
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": false
        }
    }    
  • 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'
    -d '{
        "customer": "cus_b306773fc73d23e5a9b4347984606cf7",
        "billing": "pay_automatically",
        "cancel_at_period_end": false,
        "payment_method": "card_3c000ae5db06762a2c8a00ca514fa336",
        "subscription_items": [
            {
                "plan": "plan_26aabd1d48bcfec1ff05dc2bed772aa3",
                "quantity": 3
            },
            {
                "plan": "plan_4feb4cf48e807cd142d7759a58a55d2f",
                "quantity": 5
            }
        ]
    }
    '
  • {
        "status": {
            "error_code": "",
            "status": "SUCCESS",
            "message": "",
            "response_code": "",
            "operation_id": "a84d8452-181a-4b10-8357-d991ff35ccda"
        },
        "data": {
            "id": "sub_db73da2fc2a7a3b8451fed8afdbaf46c",
            "billing": "pay_automatically",
            "billing_cycle_anchor": 1700140716,
            "created_at": 1700140716,
            "customer_token": "cus_b306773fc73d23e5a9b4347984606cf7",
            "days_until_due": 30,
            "metadata": {},
            "tax_percent": 0,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "current_period_end": 1702732716,
            "current_period_start": 1700140716,
            "discount": null,
            "ended_at": null,
            "subscription_items": {
                "data": [
                    {
                        "id": "subi_f0d375617a093d30307adec9ea246b44",
                        "created": 1700140716,
                        "metadata": null,
                        "quantity": 3,
                        "plan": {
                            "id": "plan_26aabd1d48bcfec1ff05dc2bed772aa3",
                            "aggregate_usage": "sum",
                            "amount": 10.99,
                            "billing_scheme": "per_unit",
                            "created_at": 1700140584,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {},
                            "product": "product_4ba001c7655c4a0ee4f471dbaccaa305",
                            "nickname": "Unlimited Plan plus",
                            "tiers": [],
                            "tiers_mode": "",
                            "transform_usage": {
                                "divide_by": 1,
                                "round": "up"
                            },
                            "trial_period_days": 7,
                            "usage_type": "licensed",
                            "active": true
                        }
                    },
                    {
                        "id": "subi_e3cec118feec75e0f270d888a053a410",
                        "created": 1700140716,
                        "metadata": null,
                        "quantity": 5,
                        "plan": {
                            "id": "plan_4feb4cf48e807cd142d7759a58a55d2f",
                            "aggregate_usage": "sum",
                            "amount": 9.99,
                            "billing_scheme": "per_unit",
                            "created_at": 1700140529,
                            "currency": "USD",
                            "interval": "month",
                            "interval_count": 1,
                            "metadata": {},
                            "product": "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_db73da2fc2a7a3b8451fed8afdbaf46c"
            },
            "status": "active",
            "trial_end": null,
            "trial_start": null,
            "payment_method": "card_3c000ae5db06762a2c8a00ca514fa336",
            "payment_fields": null,
            "payout_fields": null,
            "type": "payment",
            "simultaneous_invoice": false
        }
    }