Tcl Requires Chilkat v11.7.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# Requires an unlocked Chilkat bundle. See Global Unlock Sample.
# Supply a restricted sandbox key from merchant-controlled configuration.
set apiKey "YOUR_STRIPE_TEST_KEY"
# Load the saved UUID for this operation. Reuse it only for a retry with identical inputs.
set operationId "YOUR_SAVED_OPERATION_UUID"
set curl [new_CkHttpCurl]
set cmd [new_CkStringBuilder]
set json [new_CkJsonObject]
set customerId "cus_FROM_BILLING_ACCOUNT"
set priceId "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"
CkStringBuilder_SetString $cmd "curl -X POST \"https://api.stripe.com/v1/invoices\""
CkStringBuilder_Append $cmd " -u \"@KEY@:\""
CkStringBuilder_Append $cmd " -H \"Stripe-Version: 2026-10-28.endive\""
CkStringBuilder_Append $cmd " -H \"Idempotency-Key: @OP@-draft\""
CkStringBuilder_Append $cmd " -d \"customer=@CUSTOMER@\""
CkStringBuilder_Append $cmd " -d \"collection_method=send_invoice\""
CkStringBuilder_Append $cmd " -d \"days_until_due=30\""
CkStringBuilder_Append $cmd " -d \"auto_advance=false\""
CkStringBuilder_Append $cmd " -d \"pending_invoice_items_behavior=exclude\""
set replaced [CkStringBuilder_Replace $cmd "@CUSTOMER@" $customerId]
set replaced [CkStringBuilder_Replace $cmd "@OP@" $operationId]
set replaced [CkStringBuilder_Replace $cmd "@KEY@" $apiKey]
set success [CkHttpCurl_DoYourThing $curl [CkStringBuilder_getAsString $cmd]]
if {$success != 1} then {
puts "No HTTP response. Chilkat failure code: [CkHttpCurl_get_FailReason $curl]"
delete_CkHttpCurl $curl
delete_CkStringBuilder $cmd
delete_CkJsonObject $json
exit
}
set success [CkHttpCurl_GetResponseJson $curl $json]
if {expr [[CkHttpCurl_get_StatusCode $curl] != 200] || [$success != 1]} then {
puts "Stripe HTTP [CkHttpCurl_get_StatusCode $curl] Request-Id: [CkHttpCurl_getResponseHeaderField $curl Request-Id]"
if {$success == 1} then {
puts [CkJsonObject_stringOf $json error.message]
}
delete_CkHttpCurl $curl
delete_CkStringBuilder $cmd
delete_CkJsonObject $json
exit
}
set invoiceId [CkJsonObject_stringOf $json "id"]
puts "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"
CkStringBuilder_SetString $cmd "curl -X POST \"https://api.stripe.com/v1/invoiceitems\""
CkStringBuilder_Append $cmd " -u \"@KEY@:\""
CkStringBuilder_Append $cmd " -H \"Stripe-Version: 2026-10-28.endive\""
CkStringBuilder_Append $cmd " -H \"Idempotency-Key: @OP@-line-1\""
CkStringBuilder_Append $cmd " -d \"invoice=@INVOICE@\""
CkStringBuilder_Append $cmd " -d \"customer=@CUSTOMER@\""
CkStringBuilder_Append $cmd " -d \"pricing[price]=@PRICE@\""
CkStringBuilder_Append $cmd " -d \"quantity=2\""
set replaced [CkStringBuilder_Replace $cmd "@INVOICE@" $invoiceId]
set replaced [CkStringBuilder_Replace $cmd "@CUSTOMER@" $customerId]
set replaced [CkStringBuilder_Replace $cmd "@PRICE@" $priceId]
set replaced [CkStringBuilder_Replace $cmd "@OP@" $operationId]
set replaced [CkStringBuilder_Replace $cmd "@KEY@" $apiKey]
set success [CkHttpCurl_DoYourThing $curl [CkStringBuilder_getAsString $cmd]]
if {$success != 1} then {
puts "No HTTP response. Chilkat failure code: [CkHttpCurl_get_FailReason $curl]"
delete_CkHttpCurl $curl
delete_CkStringBuilder $cmd
delete_CkJsonObject $json
exit
}
set success [CkHttpCurl_GetResponseJson $curl $json]
if {expr [[CkHttpCurl_get_StatusCode $curl] != 200] || [$success != 1]} then {
puts "Stripe HTTP [CkHttpCurl_get_StatusCode $curl] Request-Id: [CkHttpCurl_getResponseHeaderField $curl Request-Id]"
if {$success == 1} then {
puts [CkJsonObject_stringOf $json error.message]
}
delete_CkHttpCurl $curl
delete_CkStringBuilder $cmd
delete_CkJsonObject $json
exit
}
puts "invoice = [CkJsonObject_stringOf $json invoice]"
puts "id = [CkJsonObject_stringOf $json id]"
puts "amount = [CkJsonObject_stringOf $json amount]"
puts "currency = [CkJsonObject_stringOf $json currency]"
delete_CkHttpCurl $curl
delete_CkStringBuilder $cmd
delete_CkJsonObject $json