Sample code for 30+ languages & platforms
Lianja

Quickbooks Send an Invoice

See more QuickBooks Examples

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

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// 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.
loJsonToken = createobject("CkJsonObject")
llSuccess = loJsonToken.LoadFile("qa_data/tokens/qb-access-token.json")

loRest = createobject("CkRest")

// Connect to the REST server.
llBTls = .T.
lnPort = 443
llBAutoReconnect = .T.
llSuccess = loRest.Connect("sandbox-quickbooks.api.intuit.com",lnPort,llBTls,llBAutoReconnect)

loSbAuth = createobject("CkStringBuilder")
loSbAuth.Append("Bearer ")
loSbAuth.Append(loJsonToken.StringOf("access_token"))
loRest.Authorization = loSbAuth.GetAsString()

// --------------------------------------------------------------------------
// 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???)
loRest.AddHeader("Content-Type","application/octet-stream")
loRest.AllowHeaderFolding = .F.

loSbResponseBody = createobject("CkStringBuilder")
llSuccess = loRest.FullRequestNoBodySb("POST","/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>",loSbResponseBody)
if (llSuccess <> .T.) then
    ? loRest.LastErrorText
    release loJsonToken
    release loRest
    release loSbAuth
    release loSbResponseBody
    return
endif

lnRespStatusCode = loRest.ResponseStatusCode

// Success is indicated by a 200 response status code.
? "response status code = " + str(lnRespStatusCode)

loJsonResponse = createobject("CkJsonObject")
loJsonResponse.LoadSb(loSbResponseBody)
loJsonResponse.EmitCompact = .F.
? loJsonResponse.Emit()

if (loRest.ResponseStatusCode <> 200) then
    ? "Failed."
    release loJsonToken
    release loRest
    release loSbAuth
    release loSbResponseBody
    release loJsonResponse
    return
endif

// 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"
// }
// 

lcInvoiceTxnDate = loJsonResponse.StringOf("Invoice.TxnDate")
lcInvoiceDomain = loJsonResponse.StringOf("Invoice.domain")
lcInvoiceCurrencyRefName = loJsonResponse.StringOf("Invoice.CurrencyRef.name")
lcInvoiceCurrencyRefValue = loJsonResponse.StringOf("Invoice.CurrencyRef.value")
lcInvoiceShipDate = loJsonResponse.StringOf("Invoice.ShipDate")
lcInvoiceTrackingNum = loJsonResponse.StringOf("Invoice.TrackingNum")
lcInvoiceClassRefName = loJsonResponse.StringOf("Invoice.ClassRef.name")
lcInvoiceClassRefValue = loJsonResponse.StringOf("Invoice.ClassRef.value")
lcInvoicePrintStatus = loJsonResponse.StringOf("Invoice.PrintStatus")
lcInvoiceSalesTermRefValue = loJsonResponse.StringOf("Invoice.SalesTermRef.value")
lcInvoiceDeliveryInfoDeliveryType = loJsonResponse.StringOf("Invoice.DeliveryInfo.DeliveryType")
lcInvoiceDeliveryInfoDeliveryTime = loJsonResponse.StringOf("Invoice.DeliveryInfo.DeliveryTime")
lcInvoiceTotalAmt = loJsonResponse.StringOf("Invoice.TotalAmt")
lcInvoiceDueDate = loJsonResponse.StringOf("Invoice.DueDate")
lcInvoiceMetaDataCreateTime = loJsonResponse.StringOf("Invoice.MetaData.CreateTime")
lcInvoiceMetaDataLastUpdatedTime = loJsonResponse.StringOf("Invoice.MetaData.LastUpdatedTime")
lcInvoiceDocNumber = loJsonResponse.StringOf("Invoice.DocNumber")
lcInvoicePrivateNote = loJsonResponse.StringOf("Invoice.PrivateNote")
llInvoiceSparse = loJsonResponse.BoolOf("Invoice.sparse")
lcInvoiceDepositToAccountRefName = loJsonResponse.StringOf("Invoice.DepositToAccountRef.name")
lcInvoiceDepositToAccountRefValue = loJsonResponse.StringOf("Invoice.DepositToAccountRef.value")
lcInvoiceCustomerMemoValue = loJsonResponse.StringOf("Invoice.CustomerMemo.value")
lcInvoiceEmailStatus = loJsonResponse.StringOf("Invoice.EmailStatus")
lcInvoiceDeposit = loJsonResponse.StringOf("Invoice.Deposit")
lcInvoiceBalance = loJsonResponse.StringOf("Invoice.Balance")
lcInvoiceCustomerRefName = loJsonResponse.StringOf("Invoice.CustomerRef.name")
lcInvoiceCustomerRefValue = loJsonResponse.StringOf("Invoice.CustomerRef.value")
lcInvoiceTxnTaxDetailTxnTaxCodeRefValue = loJsonResponse.StringOf("Invoice.TxnTaxDetail.TxnTaxCodeRef.value")
lcInvoiceTxnTaxDetailTotalTax = loJsonResponse.StringOf("Invoice.TxnTaxDetail.TotalTax")
lcInvoiceSyncToken = loJsonResponse.StringOf("Invoice.SyncToken")
lcInvoiceBillEmailAddress = loJsonResponse.StringOf("Invoice.BillEmail.Address")
lcInvoiceShipAddrCity = loJsonResponse.StringOf("Invoice.ShipAddr.City")
lcInvoiceShipAddrCountry = loJsonResponse.StringOf("Invoice.ShipAddr.Country")
lcInvoiceShipAddrLine5 = loJsonResponse.StringOf("Invoice.ShipAddr.Line5")
lcInvoiceShipAddrLine4 = loJsonResponse.StringOf("Invoice.ShipAddr.Line4")
lcInvoiceShipAddrLine3 = loJsonResponse.StringOf("Invoice.ShipAddr.Line3")
lcInvoiceShipAddrLine2 = loJsonResponse.StringOf("Invoice.ShipAddr.Line2")
lcInvoiceShipAddrLine1 = loJsonResponse.StringOf("Invoice.ShipAddr.Line1")
lcInvoiceShipAddrPostalCode = loJsonResponse.StringOf("Invoice.ShipAddr.PostalCode")
lcInvoiceShipAddrLat = loJsonResponse.StringOf("Invoice.ShipAddr.Lat")
lcInvoiceShipAddrLong = loJsonResponse.StringOf("Invoice.ShipAddr.Long")
lcInvoiceShipAddrCountrySubDivisionCode = loJsonResponse.StringOf("Invoice.ShipAddr.CountrySubDivisionCode")
lcInvoiceShipAddrId = loJsonResponse.StringOf("Invoice.ShipAddr.Id")
lcInvoiceDepartmentRefName = loJsonResponse.StringOf("Invoice.DepartmentRef.name")
lcInvoiceDepartmentRefValue = loJsonResponse.StringOf("Invoice.DepartmentRef.value")
lcInvoiceShipMethodRefName = loJsonResponse.StringOf("Invoice.ShipMethodRef.name")
lcInvoiceShipMethodRefValue = loJsonResponse.StringOf("Invoice.ShipMethodRef.value")
lcInvoiceBillAddrCity = loJsonResponse.StringOf("Invoice.BillAddr.City")
lcInvoiceBillAddrCountry = loJsonResponse.StringOf("Invoice.BillAddr.Country")
lcInvoiceBillAddrLine5 = loJsonResponse.StringOf("Invoice.BillAddr.Line5")
lcInvoiceBillAddrLine4 = loJsonResponse.StringOf("Invoice.BillAddr.Line4")
lcInvoiceBillAddrLine3 = loJsonResponse.StringOf("Invoice.BillAddr.Line3")
lcInvoiceBillAddrLine2 = loJsonResponse.StringOf("Invoice.BillAddr.Line2")
lcInvoiceBillAddrLine1 = loJsonResponse.StringOf("Invoice.BillAddr.Line1")
lcInvoiceBillAddrPostalCode = loJsonResponse.StringOf("Invoice.BillAddr.PostalCode")
lcInvoiceBillAddrLat = loJsonResponse.StringOf("Invoice.BillAddr.Lat")
lcInvoiceBillAddrLong = loJsonResponse.StringOf("Invoice.BillAddr.Long")
lcInvoiceBillAddrCountrySubDivisionCode = loJsonResponse.StringOf("Invoice.BillAddr.CountrySubDivisionCode")
lcInvoiceBillAddrId = loJsonResponse.StringOf("Invoice.BillAddr.Id")
llInvoiceApplyTaxAfterDiscount = loJsonResponse.BoolOf("Invoice.ApplyTaxAfterDiscount")
lcInvoiceId = loJsonResponse.StringOf("Invoice.Id")
lcTime = loJsonResponse.StringOf("time")
i = 0
lnCount_i = loJsonResponse.SizeOfArray("Invoice.Line")
do while i < lnCount_i
    loJsonResponse.I = i
    lcDescription = loJsonResponse.StringOf("Invoice.Line[i].Description")
    lcDetailType = loJsonResponse.StringOf("Invoice.Line[i].DetailType")
    lcSalesItemLineDetailTaxCodeRefValue = loJsonResponse.StringOf("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
    lnSalesItemLineDetailQty = loJsonResponse.IntOf("Invoice.Line[i].SalesItemLineDetail.Qty")
    lnSalesItemLineDetailUnitPrice = loJsonResponse.IntOf("Invoice.Line[i].SalesItemLineDetail.UnitPrice")
    lcSalesItemLineDetailServiceDate = loJsonResponse.StringOf("Invoice.Line[i].SalesItemLineDetail.ServiceDate")
    lcSalesItemLineDetailItemRefName = loJsonResponse.StringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
    lcSalesItemLineDetailItemRefValue = loJsonResponse.StringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
    lnLineNum = loJsonResponse.IntOf("Invoice.Line[i].LineNum")
    lcAmount = loJsonResponse.StringOf("Invoice.Line[i].Amount")
    lcId = loJsonResponse.StringOf("Invoice.Line[i].Id")
    lcDiscountLineDetailDiscountAccountRefName = loJsonResponse.StringOf("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name")
    lcDiscountLineDetailDiscountAccountRefValue = loJsonResponse.StringOf("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value")
    llDiscountLineDetailPercentBased = loJsonResponse.BoolOf("Invoice.Line[i].DiscountLineDetail.PercentBased")
    lnDiscountLineDetailDiscountPercent = loJsonResponse.IntOf("Invoice.Line[i].DiscountLineDetail.DiscountPercent")
    i = i + 1
enddo
i = 0
lnCount_i = loJsonResponse.SizeOfArray("Invoice.TxnTaxDetail.TaxLine")
do while i < lnCount_i
    loJsonResponse.I = i
    lcDetailType = loJsonResponse.StringOf("Invoice.TxnTaxDetail.TaxLine[i].DetailType")
    lcAmount = loJsonResponse.StringOf("Invoice.TxnTaxDetail.TaxLine[i].Amount")
    lcTaxLineDetailNetAmountTaxable = loJsonResponse.StringOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable")
    lnTaxLineDetailTaxPercent = loJsonResponse.IntOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent")
    lcTaxLineDetailTaxRateRefValue = loJsonResponse.StringOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value")
    llTaxLineDetailPercentBased = loJsonResponse.BoolOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased")
    i = i + 1
enddo
i = 0
lnCount_i = loJsonResponse.SizeOfArray("Invoice.CustomField")
do while i < lnCount_i
    loJsonResponse.I = i
    lcStringValue = loJsonResponse.StringOf("Invoice.CustomField[i].StringValue")
    lcType = loJsonResponse.StringOf("Invoice.CustomField[i].Type")
    lcName = loJsonResponse.StringOf("Invoice.CustomField[i].Name")
    i = i + 1
enddo


release loJsonToken
release loRest
release loSbAuth
release loSbResponseBody
release loJsonResponse