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

Stripe: Draft an invoice and attach line items

See more Stripe Examples

Create a draft for a customer and attach a priced line item to that specific invoice.

Stripe documentation and cURL example

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cApiKey
LOCAL cOperationId
LOCAL oCurl
LOCAL oCmd
LOCAL oJson
LOCAL nReplaced
LOCAL cCustomerId
LOCAL cPriceId
LOCAL cInvoiceId

nSuccess := 0

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

oCurl := CreateObject("Chilkat.HttpCurl")
oCmd := CreateObject("Chilkat.StringBuilder")
oJson := CreateObject("Chilkat.JsonObject")

cCustomerId := "cus_FROM_BILLING_ACCOUNT"

cPriceId := "price_FROM_CATALOG"

//  curl -X POST "https://api.stripe.com/v1/invoices" \
//    -u "$STRIPE_API_KEY:" \
//    -H "Stripe-Version: 2026-10-28.endive" \
//    -H "Idempotency-Key: {operationId}-draft" \
//    -d "customer={customerId}" \
//    -d "collection_method=send_invoice" \
//    -d "days_until_due=30" \
//    -d "auto_advance=false" \
//    -d "pending_invoice_items_behavior=exclude"
oCmd:SetString('curl -X POST "https://api.stripe.com/v1/invoices"')
oCmd:Append(' -u "@KEY@:"')
oCmd:Append(' -H "Stripe-Version: 2026-10-28.endive"')
oCmd:Append(' -H "Idempotency-Key: @OP@-draft"')
oCmd:Append(' -d "customer=@CUSTOMER@"')
oCmd:Append(' -d "collection_method=send_invoice"')
oCmd:Append(' -d "days_until_due=30"')
oCmd:Append(' -d "auto_advance=false"')
oCmd:Append(' -d "pending_invoice_items_behavior=exclude"')
nReplaced := oCmd:Replace("@CUSTOMER@", cCustomerId)
nReplaced := oCmd:Replace("@OP@", cOperationId)
nReplaced := oCmd:Replace("@KEY@", cApiKey)
nSuccess := oCurl:DoYourThing(oCmd:GetAsString())
IF (nSuccess != 1)
    ? "No HTTP response. Chilkat failure code: " + Str(oCurl:FailReason)
    oCurl:destroy()
    oCmd:destroy()
    oJson:destroy()
    RETURN
ENDIF

nSuccess := oCurl:GetResponseJson(oJson)
IF ((oCurl:StatusCode != 200) .OR. (nSuccess != 1))
    ? "Stripe HTTP " + Str(oCurl:StatusCode) + "  Request-Id: " + oCurl:GetResponseHeaderField("Request-Id")
    IF (nSuccess == 1)
        ? oJson:StringOf("error.message")
    ENDIF

    oCurl:destroy()
    oCmd:destroy()
    oJson:destroy()
    RETURN
ENDIF

cInvoiceId := oJson:StringOf("id")
? "Save draft invoice ID: " + cInvoiceId
//  Attach this line to the draft. Use a distinct stable key per local line on retries.

//  curl -X POST "https://api.stripe.com/v1/invoiceitems" \
//    -u "$STRIPE_API_KEY:" \
//    -H "Stripe-Version: 2026-10-28.endive" \
//    -H "Idempotency-Key: {operationId}-line-1" \
//    -d "invoice={invoiceId}" \
//    -d "customer={customerId}" \
//    -d "pricing[price]={priceId}" \
//    -d "quantity=2"
oCmd:SetString('curl -X POST "https://api.stripe.com/v1/invoiceitems"')
oCmd:Append(' -u "@KEY@:"')
oCmd:Append(' -H "Stripe-Version: 2026-10-28.endive"')
oCmd:Append(' -H "Idempotency-Key: @OP@-line-1"')
oCmd:Append(' -d "invoice=@INVOICE@"')
oCmd:Append(' -d "customer=@CUSTOMER@"')
oCmd:Append(' -d "pricing[price]=@PRICE@"')
oCmd:Append(' -d "quantity=2"')
nReplaced := oCmd:Replace("@INVOICE@", cInvoiceId)
nReplaced := oCmd:Replace("@CUSTOMER@", cCustomerId)
nReplaced := oCmd:Replace("@PRICE@", cPriceId)
nReplaced := oCmd:Replace("@OP@", cOperationId)
nReplaced := oCmd:Replace("@KEY@", cApiKey)
nSuccess := oCurl:DoYourThing(oCmd:GetAsString())
IF (nSuccess != 1)
    ? "No HTTP response. Chilkat failure code: " + Str(oCurl:FailReason)
    oCurl:destroy()
    oCmd:destroy()
    oJson:destroy()
    RETURN
ENDIF

nSuccess := oCurl:GetResponseJson(oJson)
IF ((oCurl:StatusCode != 200) .OR. (nSuccess != 1))
    ? "Stripe HTTP " + Str(oCurl:StatusCode) + "  Request-Id: " + oCurl:GetResponseHeaderField("Request-Id")
    IF (nSuccess == 1)
        ? oJson:StringOf("error.message")
    ENDIF

    oCurl:destroy()
    oCmd:destroy()
    oJson:destroy()
    RETURN
ENDIF

? "invoice = " + oJson:StringOf("invoice")
? "id = " + oJson:StringOf("id")
? "amount = " + oJson:StringOf("amount")
? "currency = " + oJson:StringOf("currency")

oCurl:destroy()
oCmd:destroy()
oJson:destroy()