Sample code for 30+ languages & platforms
Rust

Quickbooks Create an Invoice

See more QuickBooks Examples

Demonstrates how to create an invoice via 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.
// --------------------------------------------------------------------------

// Create the following JSON:

// {
//   "Line": [
//     {
//       "DetailType": "SalesItemLineDetail",
//       "Amount": 100.0,
//       "SalesItemLineDetail": {
//         "ItemRef": {
//           "name": "Services",
//           "value": "1"
//         }
//       }
//     }
//   ],
//   "CustomerRef": {
//     "value": "1"
//   }
// }
// 
// Use the this online tool to generate the code from sample JSON: 
// Generate Code to Create JSON

let json_req = chilkat::JsonObject::new();
let _ = json_req.update_string("Line[0].DetailType", "SalesItemLineDetail");
let _ = json_req.update_number("Line[0].Amount", "100.0");
let _ = json_req.update_string("Line[0].SalesItemLineDetail.ItemRef.name", "Services");
let _ = json_req.update_string("Line[0].SalesItemLineDetail.ItemRef.value", "1");
let _ = json_req.update_string("CustomerRef.value", "1");

let sb_request_body = chilkat::StringBuilder::new();
let _ = json_req.emit_sb(&sb_request_body);

let _ = rest.add_header("Content-Type", "application/json");
let _ = rest.add_header("Accept", "application/json");
rest.set_allow_header_folding(false);

let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/v3/company/<realmID>/invoice", &sb_request_body, &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": {
//     "DocNumber": "1069",
//     "SyncToken": "0",
//     "domain": "QBO",
//     "Balance": 100.0,
//     "BillAddr": {
//       "City": "Bayshore",
//       "Line1": "4581 Finch St.",
//       "PostalCode": "94326",
//       "Lat": "INVALID",
//       "Long": "INVALID",
//       "CountrySubDivisionCode": "CA",
//       "Id": "2"
//     },
//     "TxnDate": "2015-07-24",
//     "TotalAmt": 100.0,
//     "CustomerRef": {
//       "name": "Amy's Bird Sanctuary",
//       "value": "1"
//     },
//     "ShipAddr": {
//       "City": "Bayshore",
//       "Line1": "4581 Finch St.",
//       "PostalCode": "94326",
//       "Lat": "INVALID",
//       "Long": "INVALID",
//       "CountrySubDivisionCode": "CA",
//       "Id": "109"
//     },
//     "LinkedTxn": [
//     ],
//     "DueDate": "2015-08-23",
//     "PrintStatus": "NeedToPrint",
//     "Deposit": 0,
//     "sparse": false,
//     "EmailStatus": "NotSet",
//     "Line": [
//       {
//         "LineNum": 1,
//         "Amount": 100.0,
//         "SalesItemLineDetail": {
//           "TaxCodeRef": {
//             "value": "NON"
//           },
//           "ItemRef": {
//             "name": "Services",
//             "value": "1"
//           }
//         },
//         "Id": "1",
//         "DetailType": "SalesItemLineDetail"
//       },
//       {
//         "DetailType": "SubTotalLineDetail",
//         "Amount": 100.0,
//         "SubTotalLineDetail": {}
//       }
//     ],
//     "ApplyTaxAfterDiscount": false,
//     "CustomField": [
//       {
//         "DefinitionId": "1",
//         "Type": "StringType",
//         "Name": "Crew #"
//       }
//     ],
//     "Id": "238",
//     "TxnTaxDetail": {
//       "TotalTax": 0
//     },
//     "MetaData": {
//       "CreateTime": "2015-07-24T10:33:39-07:00",
//       "LastUpdatedTime": "2015-07-24T10:33:39-07:00"
//     }
//   },
//   "time": "2015-07-24T10:33:39.11-07:00"
// }
// 

let invoice_doc_number = json_response.string_of("Invoice.DocNumber").unwrap_or_default();
let invoice_sync_token = json_response.string_of("Invoice.SyncToken").unwrap_or_default();
let invoice_domain = json_response.string_of("Invoice.domain").unwrap_or_default();
let invoice_balance = json_response.string_of("Invoice.Balance").unwrap_or_default();
let invoice_bill_addr_city = json_response.string_of("Invoice.BillAddr.City").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_txn_date = json_response.string_of("Invoice.TxnDate").unwrap_or_default();
let invoice_total_amt = json_response.string_of("Invoice.TotalAmt").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_ship_addr_city = json_response.string_of("Invoice.ShipAddr.City").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_due_date = json_response.string_of("Invoice.DueDate").unwrap_or_default();
let invoice_print_status = json_response.string_of("Invoice.PrintStatus").unwrap_or_default();
let invoice_deposit = json_response.int_of("Invoice.Deposit");
let invoice_sparse = json_response.bool_of("Invoice.sparse");
let invoice_email_status = json_response.string_of("Invoice.EmailStatus").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 invoice_txn_tax_detail_total_tax = json_response.int_of("Invoice.TxnTaxDetail.TotalTax");
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 time = json_response.string_of("time").unwrap_or_default();
let mut i = 0;
let mut count_i = json_response.size_of_array("Invoice.LinkedTxn");
while i < count_i {
    json_response.set_i(i);
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("Invoice.Line");
while i < count_i {
    json_response.set_i(i);
    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].SalesItemLineDetail.TaxCodeRef.value").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.string_of("Invoice.Line[i].Id").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].DetailType").unwrap_or_default();
    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].DefinitionId").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;
}