Rust Requires Chilkat v11.7.0+
Rust
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 Rust Downloads
let mut success = false;
// Requires an unlocked Chilkat bundle. See Global Unlock Sample.
// Supply a restricted sandbox key from merchant-controlled configuration.
let api_key = "YOUR_STRIPE_TEST_KEY".to_string();
// Load the saved UUID for this operation. Reuse it only for a retry with identical inputs.
let operation_id = "YOUR_SAVED_OPERATION_UUID".to_string();
let curl = chilkat::HttpCurl::new();
let cmd = chilkat::StringBuilder::new();
let json = chilkat::JsonObject::new();
let customer_id = "cus_FROM_BILLING_ACCOUNT".to_string();
let price_id = "price_FROM_CATALOG".to_string();
// 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"
let _ = cmd.set_string("curl -X POST \"https://api.stripe.com/v1/invoices\"");
let _ = cmd.append(" -u \"@KEY@:\"");
let _ = cmd.append(" -H \"Stripe-Version: 2026-10-28.endive\"");
let _ = cmd.append(" -H \"Idempotency-Key: @OP@-draft\"");
let _ = cmd.append(" -d \"customer=@CUSTOMER@\"");
let _ = cmd.append(" -d \"collection_method=send_invoice\"");
let _ = cmd.append(" -d \"days_until_due=30\"");
let _ = cmd.append(" -d \"auto_advance=false\"");
let _ = cmd.append(" -d \"pending_invoice_items_behavior=exclude\"");
let _ = cmd.replace("@CUSTOMER@", &customer_id);
let _ = cmd.replace("@OP@", &operation_id);
let _ = cmd.replace("@KEY@", &api_key);
success = curl.do_your_thing(&cmd.get_as_string().unwrap_or_default()).is_ok();
if !success {
println!("No HTTP response. Chilkat failure code: {}", curl.fail_reason());
return;
}
success = curl.get_response_json(&json).is_ok();
if (curl.status_code() != 200) || (!success) {
println!("Stripe HTTP {} Request-Id: {}", curl.status_code(), curl.get_response_header_field("Request-Id").unwrap_or_default());
if success {
println!("{}", json.string_of("error.message").unwrap_or_default());
}
return;
}
let invoice_id = json.string_of("id").unwrap_or_default();
println!("Save draft invoice ID: {}", invoice_id);
// 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"
let _ = cmd.set_string("curl -X POST \"https://api.stripe.com/v1/invoiceitems\"");
let _ = cmd.append(" -u \"@KEY@:\"");
let _ = cmd.append(" -H \"Stripe-Version: 2026-10-28.endive\"");
let _ = cmd.append(" -H \"Idempotency-Key: @OP@-line-1\"");
let _ = cmd.append(" -d \"invoice=@INVOICE@\"");
let _ = cmd.append(" -d \"customer=@CUSTOMER@\"");
let _ = cmd.append(" -d \"pricing[price]=@PRICE@\"");
let _ = cmd.append(" -d \"quantity=2\"");
let _ = cmd.replace("@INVOICE@", &invoice_id);
let _ = cmd.replace("@CUSTOMER@", &customer_id);
let _ = cmd.replace("@PRICE@", &price_id);
let _ = cmd.replace("@OP@", &operation_id);
let _ = cmd.replace("@KEY@", &api_key);
success = curl.do_your_thing(&cmd.get_as_string().unwrap_or_default()).is_ok();
if !success {
println!("No HTTP response. Chilkat failure code: {}", curl.fail_reason());
return;
}
success = curl.get_response_json(&json).is_ok();
if (curl.status_code() != 200) || (!success) {
println!("Stripe HTTP {} Request-Id: {}", curl.status_code(), curl.get_response_header_field("Request-Id").unwrap_or_default());
if success {
println!("{}", json.string_of("error.message").unwrap_or_default());
}
return;
}
println!("invoice = {}", json.string_of("invoice").unwrap_or_default());
println!("id = {}", json.string_of("id").unwrap_or_default());
println!("amount = {}", json.string_of("amount").unwrap_or_default());
println!("currency = {}", json.string_of("currency").unwrap_or_default());