Sample code for 30+ languages & platforms
Rust

Quickbooks Send an Invoice

See more QuickBooks Examples

Demonstrates how to send an invoice using the Quickbooks REST API.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// First get our previously obtained OAuth2 access token.
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/qb-access-token.json").is_ok();

let rest = chilkat::Rest::new();

// Connect to the REST server.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("sandbox-quickbooks.api.intuit.com", port, b_tls, b_auto_reconnect).is_ok();

let sb_auth = chilkat::StringBuilder::new();
let _ = sb_auth.append("Bearer ");
let _ = sb_auth.append(&json_token.string_of("access_token").unwrap_or_default());
rest.set_authorization(&sb_auth.get_as_string().unwrap_or_default());

// --------------------------------------------------------------------------
// Note: The above code to setup the initial REST connection
// can be done once.  After connecting, any number of REST calls can be made.
// If the connection is lost, the next REST method call will automatically
// reconnect if needed.
// --------------------------------------------------------------------------

// Technically, the POST has an empty request body, but the Quickbooks documentation indicates that 
// the Content-Type header should be set to "application/octet-stream", which really makes no sense
// because there is not content. (How can no content have a type???)
let _ = rest.add_header("Content-Type", "application/octet-stream");
rest.set_allow_header_folding(false);

let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_no_body_sb("POST", "/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>", &sb_response_body).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let resp_status_code = rest.response_status_code();

// Success is indicated by a 200 response status code.
println!("response status code = {}", resp_status_code);

let json_response = chilkat::JsonObject::new();
let _ = json_response.load_sb(&sb_response_body);
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

if rest.response_status_code() != 200 {
    println!("Failed.");
    return;
}

// Sample output...
// (See the parsing code below..)
// 
// Use the this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "Invoice": {
//     "TxnDate": "2013-03-14",
//     "domain": "QBO",
//     "CurrencyRef": {
//       "name": "United States Dollar",
//       "value": "USD"
//     },
//     "ShipDate": "2013-03-01",
//     "TrackingNum": "123456789",
//     "ClassRef": {
//       "name": "Class 1",
//       "value": "200900000000000003901"
//     },
//     "PrintStatus": "NeedToPrint",
//     "SalesTermRef": {
//       "value": "4"
//     },
//     "DeliveryInfo": {
//       "DeliveryType": "Email",
//       "DeliveryTime": "2014-12-17T11:50:52-08:00"
//     },
//     "TotalAmt": 52.0,
//     "Line": [
//       {
//         "Description": "Sample invoice create request",
//         "DetailType": "SalesItemLineDetail",
//         "SalesItemLineDetail": {
//           "TaxCodeRef": {
//             "value": "TAX"
//           },
//           "Qty": 1,
//           "UnitPrice": 50,
//           "ServiceDate": "2013-03-04",
//           "ItemRef": {
//             "name": "Hours",
//             "value": "2"
//           }
//         },
//         "LineNum": 1,
//         "Amount": 50.0,
//         "Id": "1"
//       },
//       {
//         "DetailType": "SubTotalLineDetail",
//         "Amount": 50.0,
//         "SubTotalLineDetail": {}
//       },
//       {
//         "DetailType": "DiscountLineDetail",
//         "Amount": 5.0,
//         "DiscountLineDetail": {
//           "DiscountAccountRef": {
//             "name": "Discounts given",
//             "value": "30"
//           },
//           "PercentBased": true,
//           "DiscountPercent": 10
//         }
//       },
//       {
//         "DetailType": "SalesItemLineDetail",
//         "Amount": 2.0,
//         "SalesItemLineDetail": {
//           "ItemRef": {
//             "value": "SHIPPING_ITEM_ID"
//           }
//         }
//       }
//     ],
//     "DueDate": "2013-05-13",
//     "MetaData": {
//       "CreateTime": "2013-03-14T01:42:16-07:00",
//       "LastUpdatedTime": "2014-12-17T11:50:58-08:00"
//     },
//     "DocNumber": "Sample_Inv#2",
//     "PrivateNote": "Summary for sample invoice",
//     "sparse": false,
//     "DepositToAccountRef": {
//       "name": "Undeposited Funds",
//       "value": "4"
//     },
//     "CustomerMemo": {
//       "value": "This is the customer message"
//     },
//     "EmailStatus": "EmailSent",
//     "Deposit": 12.0,
//     "Balance": 40.0,
//     "CustomerRef": {
//       "name": "Mr V3 Service Customer Jr2",
//       "value": "15"
//     },
//     "TxnTaxDetail": {
//       "TxnTaxCodeRef": {
//         "value": "5"
//       },
//       "TotalTax": 5.0,
//       "TaxLine": [
//         {
//           "DetailType": "TaxLineDetail",
//           "Amount": 5.0,
//           "TaxLineDetail": {
//             "NetAmountTaxable": 50.0,
//             "TaxPercent": 10,
//             "TaxRateRef": {
//               "value": "2"
//             },
//             "PercentBased": true
//           }
//         }
//       ]
//     },
//     "SyncToken": "0",
//     "BillEmail": {
//       "Address": "test@intuit.com"
//     },
//     "ShipAddr": {
//       "City": "San Jose",
//       "Country": "USA",
//       "Line5": "Cube 999",
//       "Line4": "Dept 12",
//       "Line3": "123 street",
//       "Line2": "Building 1",
//       "Line1": "Intuit",
//       "PostalCode": "95123",
//       "Lat": "37.2374847",
//       "Long": "-121.8277925",
//       "CountrySubDivisionCode": "CA",
//       "Id": "36"
//     },
//     "DepartmentRef": {
//       "name": "Mountain View",
//       "value": "1"
//     },
//     "ShipMethodRef": {
//       "name": "UPS",
//       "value": "UPS"
//     },
//     "BillAddr": {
//       "City": "Mountain View",
//       "Country": "USA",
//       "Line5": "Cube 999",
//       "Line4": "Dept 12",
//       "Line3": "123 street",
//       "Line2": "Building 1",
//       "Line1": "Google",
//       "PostalCode": "95123",
//       "Lat": "37.2374847",
//       "Long": "-121.8277925",
//       "CountrySubDivisionCode": "CA",
//       "Id": "35"
//     },
//     "ApplyTaxAfterDiscount": false,
//     "CustomField": [
//       {
//         "StringValue": "Custom1",
//         "Type": "StringType",
//         "Name": "Custom 1"
//       },
//       {
//         "StringValue": "Custom2",
//         "Type": "StringType",
//         "Name": "Custom 2"
//       },
//       {
//         "StringValue": "Custom3",
//         "Type": "StringType",
//         "Name": "Custom 3"
//       }
//     ],
//     "Id": "96"
//   },
//   "time": "2013-03-14T13:32:04.895-07:00"
// }
// 

let invoice_txn_date = json_response.string_of("Invoice.TxnDate").unwrap_or_default();
let invoice_domain = json_response.string_of("Invoice.domain").unwrap_or_default();
let invoice_currency_ref_name = json_response.string_of("Invoice.CurrencyRef.name").unwrap_or_default();
let invoice_currency_ref_value = json_response.string_of("Invoice.CurrencyRef.value").unwrap_or_default();
let invoice_ship_date = json_response.string_of("Invoice.ShipDate").unwrap_or_default();
let invoice_tracking_num = json_response.string_of("Invoice.TrackingNum").unwrap_or_default();
let invoice_class_ref_name = json_response.string_of("Invoice.ClassRef.name").unwrap_or_default();
let invoice_class_ref_value = json_response.string_of("Invoice.ClassRef.value").unwrap_or_default();
let invoice_print_status = json_response.string_of("Invoice.PrintStatus").unwrap_or_default();
let invoice_sales_term_ref_value = json_response.string_of("Invoice.SalesTermRef.value").unwrap_or_default();
let invoice_delivery_info_delivery_type = json_response.string_of("Invoice.DeliveryInfo.DeliveryType").unwrap_or_default();
let invoice_delivery_info_delivery_time = json_response.string_of("Invoice.DeliveryInfo.DeliveryTime").unwrap_or_default();
let invoice_total_amt = json_response.string_of("Invoice.TotalAmt").unwrap_or_default();
let invoice_due_date = json_response.string_of("Invoice.DueDate").unwrap_or_default();
let invoice_meta_data_create_time = json_response.string_of("Invoice.MetaData.CreateTime").unwrap_or_default();
let invoice_meta_data_last_updated_time = json_response.string_of("Invoice.MetaData.LastUpdatedTime").unwrap_or_default();
let invoice_doc_number = json_response.string_of("Invoice.DocNumber").unwrap_or_default();
let invoice_private_note = json_response.string_of("Invoice.PrivateNote").unwrap_or_default();
let invoice_sparse = json_response.bool_of("Invoice.sparse");
let invoice_deposit_to_account_ref_name = json_response.string_of("Invoice.DepositToAccountRef.name").unwrap_or_default();
let invoice_deposit_to_account_ref_value = json_response.string_of("Invoice.DepositToAccountRef.value").unwrap_or_default();
let invoice_customer_memo_value = json_response.string_of("Invoice.CustomerMemo.value").unwrap_or_default();
let invoice_email_status = json_response.string_of("Invoice.EmailStatus").unwrap_or_default();
let invoice_deposit = json_response.string_of("Invoice.Deposit").unwrap_or_default();
let invoice_balance = json_response.string_of("Invoice.Balance").unwrap_or_default();
let invoice_customer_ref_name = json_response.string_of("Invoice.CustomerRef.name").unwrap_or_default();
let invoice_customer_ref_value = json_response.string_of("Invoice.CustomerRef.value").unwrap_or_default();
let invoice_txn_tax_detail_txn_tax_code_ref_value = json_response.string_of("Invoice.TxnTaxDetail.TxnTaxCodeRef.value").unwrap_or_default();
let invoice_txn_tax_detail_total_tax = json_response.string_of("Invoice.TxnTaxDetail.TotalTax").unwrap_or_default();
let invoice_sync_token = json_response.string_of("Invoice.SyncToken").unwrap_or_default();
let invoice_bill_email_address = json_response.string_of("Invoice.BillEmail.Address").unwrap_or_default();
let invoice_ship_addr_city = json_response.string_of("Invoice.ShipAddr.City").unwrap_or_default();
let invoice_ship_addr_country = json_response.string_of("Invoice.ShipAddr.Country").unwrap_or_default();
let invoice_ship_addr_line5 = json_response.string_of("Invoice.ShipAddr.Line5").unwrap_or_default();
let invoice_ship_addr_line4 = json_response.string_of("Invoice.ShipAddr.Line4").unwrap_or_default();
let invoice_ship_addr_line3 = json_response.string_of("Invoice.ShipAddr.Line3").unwrap_or_default();
let invoice_ship_addr_line2 = json_response.string_of("Invoice.ShipAddr.Line2").unwrap_or_default();
let invoice_ship_addr_line1 = json_response.string_of("Invoice.ShipAddr.Line1").unwrap_or_default();
let invoice_ship_addr_postal_code = json_response.string_of("Invoice.ShipAddr.PostalCode").unwrap_or_default();
let invoice_ship_addr_lat = json_response.string_of("Invoice.ShipAddr.Lat").unwrap_or_default();
let invoice_ship_addr_long = json_response.string_of("Invoice.ShipAddr.Long").unwrap_or_default();
let invoice_ship_addr_country_sub_division_code = json_response.string_of("Invoice.ShipAddr.CountrySubDivisionCode").unwrap_or_default();
let invoice_ship_addr_id = json_response.string_of("Invoice.ShipAddr.Id").unwrap_or_default();
let invoice_department_ref_name = json_response.string_of("Invoice.DepartmentRef.name").unwrap_or_default();
let invoice_department_ref_value = json_response.string_of("Invoice.DepartmentRef.value").unwrap_or_default();
let invoice_ship_method_ref_name = json_response.string_of("Invoice.ShipMethodRef.name").unwrap_or_default();
let invoice_ship_method_ref_value = json_response.string_of("Invoice.ShipMethodRef.value").unwrap_or_default();
let invoice_bill_addr_city = json_response.string_of("Invoice.BillAddr.City").unwrap_or_default();
let invoice_bill_addr_country = json_response.string_of("Invoice.BillAddr.Country").unwrap_or_default();
let invoice_bill_addr_line5 = json_response.string_of("Invoice.BillAddr.Line5").unwrap_or_default();
let invoice_bill_addr_line4 = json_response.string_of("Invoice.BillAddr.Line4").unwrap_or_default();
let invoice_bill_addr_line3 = json_response.string_of("Invoice.BillAddr.Line3").unwrap_or_default();
let invoice_bill_addr_line2 = json_response.string_of("Invoice.BillAddr.Line2").unwrap_or_default();
let invoice_bill_addr_line1 = json_response.string_of("Invoice.BillAddr.Line1").unwrap_or_default();
let invoice_bill_addr_postal_code = json_response.string_of("Invoice.BillAddr.PostalCode").unwrap_or_default();
let invoice_bill_addr_lat = json_response.string_of("Invoice.BillAddr.Lat").unwrap_or_default();
let invoice_bill_addr_long = json_response.string_of("Invoice.BillAddr.Long").unwrap_or_default();
let invoice_bill_addr_country_sub_division_code = json_response.string_of("Invoice.BillAddr.CountrySubDivisionCode").unwrap_or_default();
let invoice_bill_addr_id = json_response.string_of("Invoice.BillAddr.Id").unwrap_or_default();
let invoice_apply_tax_after_discount = json_response.bool_of("Invoice.ApplyTaxAfterDiscount");
let invoice_id = json_response.string_of("Invoice.Id").unwrap_or_default();
let time = json_response.string_of("time").unwrap_or_default();
let mut i = 0;
let mut count_i = json_response.size_of_array("Invoice.Line");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("Invoice.Line[i].Description").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].DetailType").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value").unwrap_or_default();
    let _ = json_response.int_of("Invoice.Line[i].SalesItemLineDetail.Qty");
    let _ = json_response.int_of("Invoice.Line[i].SalesItemLineDetail.UnitPrice");
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.ServiceDate").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.name").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.value").unwrap_or_default();
    let _ = json_response.int_of("Invoice.Line[i].LineNum");
    let _ = json_response.string_of("Invoice.Line[i].Amount").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].Id").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value").unwrap_or_default();
    let _ = json_response.bool_of("Invoice.Line[i].DiscountLineDetail.PercentBased");
    let _ = json_response.int_of("Invoice.Line[i].DiscountLineDetail.DiscountPercent");
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("Invoice.TxnTaxDetail.TaxLine");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("Invoice.TxnTaxDetail.TaxLine[i].DetailType").unwrap_or_default();
    let _ = json_response.string_of("Invoice.TxnTaxDetail.TaxLine[i].Amount").unwrap_or_default();
    let _ = json_response.string_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable").unwrap_or_default();
    let _ = json_response.int_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent");
    let _ = json_response.string_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value").unwrap_or_default();
    let _ = json_response.bool_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased");
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("Invoice.CustomField");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("Invoice.CustomField[i].StringValue").unwrap_or_default();
    let _ = json_response.string_of("Invoice.CustomField[i].Type").unwrap_or_default();
    let _ = json_response.string_of("Invoice.CustomField[i].Name").unwrap_or_default();
    i = i + 1;
}