Sample code for 30+ languages & platforms
C Requires Chilkat v11.7.0+

Stripe: Start a subscription billed by invoice

See more Stripe Examples

Start an invoice-billed subscription for an existing customer, using a recurring price and 30-day payment terms.

Stripe documentation and cURL example

Chilkat C Downloads

C
#include <C_CkHttpCurl.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *apiKey;
    const char *operationId;
    HCkHttpCurl curl;
    HCkStringBuilder cmd;
    HCkJsonObject json;
    int replaced;
    const char *customerId;
    const char *priceId;

    success = FALSE;

    //  Requires an unlocked Chilkat bundle. See Global Unlock Sample.
    //  Supply a restricted sandbox key from merchant-controlled configuration.
    apiKey = "YOUR_STRIPE_TEST_KEY";
    //  Load the saved UUID for this operation. Reuse it only for a retry with identical inputs.
    operationId = "YOUR_SAVED_OPERATION_UUID";

    curl = CkHttpCurl_Create();
    cmd = CkStringBuilder_Create();
    json = CkJsonObject_Create();

    customerId = "cus_FROM_BILLING_ACCOUNT";

    priceId = "price_FROM_CATALOG";

    //  Choose a recurring price. The customer pays the invoice under the agreed terms.

    //  curl -X POST "https://api.stripe.com/v1/subscriptions" \
    //    -u "$STRIPE_API_KEY:" \
    //    -H "Stripe-Version: 2026-10-28.endive" \
    //    -H "Idempotency-Key: {operationId}-subscription" \
    //    -d "customer={customerId}" \
    //    -d "items[0][price]={priceId}" \
    //    -d "items[0][quantity]=3" \
    //    -d "collection_method=send_invoice" \
    //    -d "days_until_due=30"
    CkStringBuilder_SetString(cmd,"curl -X POST \"https://api.stripe.com/v1/subscriptions\"");
    CkStringBuilder_Append(cmd," -u \"@KEY@:\"");
    CkStringBuilder_Append(cmd," -H \"Stripe-Version: 2026-10-28.endive\"");
    CkStringBuilder_Append(cmd," -H \"Idempotency-Key: @OP@-subscription\"");
    CkStringBuilder_Append(cmd," -d \"customer=@CUSTOMER@\"");
    CkStringBuilder_Append(cmd," -d \"items[0][price]=@PRICE@\"");
    CkStringBuilder_Append(cmd," -d \"items[0][quantity]=3\"");
    CkStringBuilder_Append(cmd," -d \"collection_method=send_invoice\"");
    CkStringBuilder_Append(cmd," -d \"days_until_due=30\"");
    replaced = CkStringBuilder_Replace(cmd,"@CUSTOMER@",customerId);
    replaced = CkStringBuilder_Replace(cmd,"@PRICE@",priceId);
    replaced = CkStringBuilder_Replace(cmd,"@OP@",operationId);
    replaced = CkStringBuilder_Replace(cmd,"@KEY@",apiKey);
    success = CkHttpCurl_DoYourThing(curl,CkStringBuilder_getAsString(cmd));
    if (success != TRUE) {
        printf("No HTTP response. Chilkat failure code: %d\n",CkHttpCurl_getFailReason(curl));
        CkHttpCurl_Dispose(curl);
        CkStringBuilder_Dispose(cmd);
        CkJsonObject_Dispose(json);
        return;
    }

    success = CkHttpCurl_GetResponseJson(curl,json);
    if ((CkHttpCurl_getStatusCode(curl) != 200) || (success != TRUE)) {
        printf("Stripe HTTP %d  Request-Id: %s\n",CkHttpCurl_getStatusCode(curl),CkHttpCurl_getResponseHeaderField(curl,"Request-Id"));
        if (success == TRUE) {
            printf("%s\n",CkJsonObject_stringOf(json,"error.message"));
        }

        CkHttpCurl_Dispose(curl);
        CkStringBuilder_Dispose(cmd);
        CkJsonObject_Dispose(json);
        return;
    }

    printf("id = %s\n",CkJsonObject_stringOf(json,"id"));
    printf("status = %s\n",CkJsonObject_stringOf(json,"status"));
    printf("latest_invoice = %s\n",CkJsonObject_stringOf(json,"latest_invoice"));

    //  Save the subscription and invoice IDs. Read payment status from the invoice.


    CkHttpCurl_Dispose(curl);
    CkStringBuilder_Dispose(cmd);
    CkJsonObject_Dispose(json);

    }