Sample code for 30+ languages & platforms
Swift 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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let curl = CkoHttpCurl()!
    let cmd = CkoStringBuilder()!
    let json = CkoJsonObject()!

    var replaced: Int

    var customerId: String? = "cus_FROM_BILLING_ACCOUNT"

    var priceId: String? = "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"
    cmd.setString(value: "curl -X POST \"https://api.stripe.com/v1/invoices\"")
    cmd.append(value: " -u \"@KEY@:\"")
    cmd.append(value: " -H \"Stripe-Version: 2026-10-28.endive\"")
    cmd.append(value: " -H \"Idempotency-Key: @OP@-draft\"")
    cmd.append(value: " -d \"customer=@CUSTOMER@\"")
    cmd.append(value: " -d \"collection_method=send_invoice\"")
    cmd.append(value: " -d \"days_until_due=30\"")
    cmd.append(value: " -d \"auto_advance=false\"")
    cmd.append(value: " -d \"pending_invoice_items_behavior=exclude\"")
    replaced = cmd.replace(value: "@CUSTOMER@", replacement: customerId).intValue
    replaced = cmd.replace(value: "@OP@", replacement: operationId).intValue
    replaced = cmd.replace(value: "@KEY@", replacement: apiKey).intValue
    success = curl.doYourThing(targetCurl: cmd.getAsString())
    if success != true {
        print("No HTTP response. Chilkat failure code: \(curl.failReason.intValue)")
        return
    }

    success = curl.getResponseJson(json: json)
    if (curl.statusCode.intValue != 200) || (success != true) {
        print("Stripe HTTP \(curl.statusCode.intValue)  Request-Id: \(curl.getResponseHeaderField(name: "Request-Id")!)")
        if success == true {
            print("\(json.string(of: "error.message")!)")
        }

        return
    }

    var invoiceId: String? = json.string(of: "id")
    print("Save draft invoice ID: \(invoiceId!)")
    //  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"
    cmd.setString(value: "curl -X POST \"https://api.stripe.com/v1/invoiceitems\"")
    cmd.append(value: " -u \"@KEY@:\"")
    cmd.append(value: " -H \"Stripe-Version: 2026-10-28.endive\"")
    cmd.append(value: " -H \"Idempotency-Key: @OP@-line-1\"")
    cmd.append(value: " -d \"invoice=@INVOICE@\"")
    cmd.append(value: " -d \"customer=@CUSTOMER@\"")
    cmd.append(value: " -d \"pricing[price]=@PRICE@\"")
    cmd.append(value: " -d \"quantity=2\"")
    replaced = cmd.replace(value: "@INVOICE@", replacement: invoiceId).intValue
    replaced = cmd.replace(value: "@CUSTOMER@", replacement: customerId).intValue
    replaced = cmd.replace(value: "@PRICE@", replacement: priceId).intValue
    replaced = cmd.replace(value: "@OP@", replacement: operationId).intValue
    replaced = cmd.replace(value: "@KEY@", replacement: apiKey).intValue
    success = curl.doYourThing(targetCurl: cmd.getAsString())
    if success != true {
        print("No HTTP response. Chilkat failure code: \(curl.failReason.intValue)")
        return
    }

    success = curl.getResponseJson(json: json)
    if (curl.statusCode.intValue != 200) || (success != true) {
        print("Stripe HTTP \(curl.statusCode.intValue)  Request-Id: \(curl.getResponseHeaderField(name: "Request-Id")!)")
        if success == true {
            print("\(json.string(of: "error.message")!)")
        }

        return
    }

    print("invoice = \(json.string(of: "invoice")!)")
    print("id = \(json.string(of: "id")!)")
    print("amount = \(json.string(of: "amount")!)")
    print("currency = \(json.string(of: "currency")!)")

}