Sample code for 30+ languages & platforms
Rust

Quickbooks Query an Invoice

See more QuickBooks Examples

Demonstrates how to query for invoices matching a SELECT statement 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();

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 REST connection and setup of the AWS credentials
// can be done once.  After connecting, any number of REST calls can be made.
// The "auto reconnect" property passed to rest.Connect indicates that if
// the connection is lost, a REST method call will automatically reconnect
// if needed.
// --------------------------------------------------------------------------

// This is a GET request, so there should be no Content-Type
// This line of code is just to make sure..
let _ = rest.remove_header("Content-Type");

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

// Add a SELECT statement 
let _ = rest.add_query_param("query", "select * from Invoice where id = '239'");

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

let resp_status_code = rest.response_status_code();
if resp_status_code >= 400 {
    println!("Response Status Code = {}", resp_status_code);
    println!("Response Header:");
    println!("{}", rest.response_header());
    println!("Response Body:");
    println!("{}", sb_response_body.get_as_string().unwrap_or_default());
    return;
}

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

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

// Sample output:
// Use the this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "QueryResponse": {
//     "startPosition": 1, 
//     "totalCount": 1, 
//     "maxResults": 1, 
//     "Invoice": [
//       {
//         "DocNumber": "1070", 
//         "SyncToken": "0", 
//         "domain": "QBO", 
//         "Balance": 150.0, 
//         "BillAddr": {
//           "City": "Bayshore", 
//           "Line1": "4581 Finch St.", 
//           "PostalCode": "94326", 
//           "Lat": "INVALID", 
//           "Long": "INVALID", 
//           "CountrySubDivisionCode": "CA", 
//           "Id": "2"
//         }, 
//         "TxnDate": "2015-07-24", 
//         "TotalAmt": 150.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": 150.0, 
//             "SalesItemLineDetail": {
//               "TaxCodeRef": {
//                 "value": "NON"
//               }, 
//               "ItemRef": {
//                 "name": "Services", 
//                 "value": "1"
//               }
//             }, 
//             "Id": "1", 
//             "DetailType": "SalesItemLineDetail"
//           }, 
//           {
//             "DetailType": "SubTotalLineDetail", 
//             "Amount": 150.0, 
//             "SubTotalLineDetail": {}
//           }
//         ], 
//         "ApplyTaxAfterDiscount": false, 
//         "CustomField": [
//           {
//             "DefinitionId": "1", 
//             "Type": "StringType", 
//             "Name": "Crew #"
//           }
//         ], 
//         "Id": "239", 
//         "TxnTaxDetail": {
//           "TotalTax": 0
//         }, 
//         "MetaData": {
//           "CreateTime": "2015-07-24T10:35:08-07:00", 
//           "LastUpdatedTime": "2015-07-24T10:35:08-07:00"
//         }
//       }
//     ]
//   }, 
//   "time": "2015-07-24T10:38:50.01-07:00"
// }

let mut j: i32 = 0;
let mut count_j: i32 = 0;

let query_response_start_position = json.int_of("QueryResponse.startPosition");
let query_response_total_count = json.int_of("QueryResponse.totalCount");
let query_response_max_results = json.int_of("QueryResponse.maxResults");
let time = json.string_of("time").unwrap_or_default();
let mut i = 0;
let count_i = json.size_of_array("QueryResponse.Invoice");
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("QueryResponse.Invoice[i].DocNumber").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].SyncToken").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].domain").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].Balance").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.City").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.Line1").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.PostalCode").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.Lat").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.Long").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].BillAddr.Id").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].TxnDate").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].TotalAmt").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].CustomerRef.name").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].CustomerRef.value").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.City").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.Line1").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.PostalCode").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.Lat").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.Long").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].ShipAddr.Id").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].DueDate").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].PrintStatus").unwrap_or_default();
    let _ = json.int_of("QueryResponse.Invoice[i].Deposit");
    let _ = json.bool_of("QueryResponse.Invoice[i].sparse");
    let _ = json.string_of("QueryResponse.Invoice[i].EmailStatus").unwrap_or_default();
    let _ = json.bool_of("QueryResponse.Invoice[i].ApplyTaxAfterDiscount");
    let _ = json.string_of("QueryResponse.Invoice[i].Id").unwrap_or_default();
    let _ = json.int_of("QueryResponse.Invoice[i].TxnTaxDetail.TotalTax");
    let _ = json.string_of("QueryResponse.Invoice[i].MetaData.CreateTime").unwrap_or_default();
    let _ = json.string_of("QueryResponse.Invoice[i].MetaData.LastUpdatedTime").unwrap_or_default();
    j = 0;
    count_j = json.size_of_array("QueryResponse.Invoice[i].LinkedTxn");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("QueryResponse.Invoice[i].Line");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("QueryResponse.Invoice[i].Line[j].LineNum");
        let _ = json.string_of("QueryResponse.Invoice[i].Line[j].Amount").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].Line[j].Id").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].Line[j].DetailType").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("QueryResponse.Invoice[i].CustomField");
    while j < count_j {
        json.set_j(j);
        let _ = json.string_of("QueryResponse.Invoice[i].CustomField[j].DefinitionId").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].CustomField[j].Type").unwrap_or_default();
        let _ = json.string_of("QueryResponse.Invoice[i].CustomField[j].Name").unwrap_or_default();
        j = j + 1;
    }

    i = i + 1;
}