Sample code for 30+ languages & platforms
Delphi DLL

Quickbooks Create an Invoice

See more QuickBooks Examples

Demonstrates how to create an invoice via the Quickbooks REST API.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Rest, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
jsonToken: HCkJsonObject;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
sbAuth: HCkStringBuilder;
jsonReq: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
LineNum: Integer;
Amount: PWideChar;
SalesItemLineDetailTaxCodeRefValue: PWideChar;
SalesItemLineDetailItemRefName: PWideChar;
SalesItemLineDetailItemRefValue: PWideChar;
Id: PWideChar;
DetailType: PWideChar;
DefinitionId: PWideChar;
invType: PWideChar;
Name: PWideChar;
InvoiceDocNumber: PWideChar;
InvoiceSyncToken: PWideChar;
InvoiceDomain: PWideChar;
InvoiceBalance: PWideChar;
InvoiceBillAddrCity: PWideChar;
InvoiceBillAddrLine1: PWideChar;
InvoiceBillAddrPostalCode: PWideChar;
InvoiceBillAddrLat: PWideChar;
InvoiceBillAddrLong: PWideChar;
InvoiceBillAddrCountrySubDivisionCode: PWideChar;
InvoiceBillAddrId: PWideChar;
InvoiceTxnDate: PWideChar;
InvoiceTotalAmt: PWideChar;
InvoiceCustomerRefName: PWideChar;
InvoiceCustomerRefValue: PWideChar;
InvoiceShipAddrCity: PWideChar;
InvoiceShipAddrLine1: PWideChar;
InvoiceShipAddrPostalCode: PWideChar;
InvoiceShipAddrLat: PWideChar;
InvoiceShipAddrLong: PWideChar;
InvoiceShipAddrCountrySubDivisionCode: PWideChar;
InvoiceShipAddrId: PWideChar;
InvoiceDueDate: PWideChar;
InvoicePrintStatus: PWideChar;
InvoiceDeposit: Integer;
InvoiceSparse: Boolean;
InvoiceEmailStatus: PWideChar;
InvoiceApplyTaxAfterDiscount: Boolean;
InvoiceId: PWideChar;
InvoiceTxnTaxDetailTotalTax: Integer;
InvoiceMetaDataCreateTime: PWideChar;
InvoiceMetaDataLastUpdatedTime: PWideChar;
time: PWideChar;
i: Integer;
count_i: Integer;

begin
success := False;

// 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.
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/qb-access-token.json');

rest := CkRest_Create();

// Connect to the REST server.
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'sandbox-quickbooks.api.intuit.com',port,bTls,bAutoReconnect);

sbAuth := CkStringBuilder_Create();
CkStringBuilder_Append(sbAuth,'Bearer ');
CkStringBuilder_Append(sbAuth,CkJsonObject__stringOf(jsonToken,'access_token'));
CkRest_putAuthorization(rest,CkStringBuilder__getAsString(sbAuth));

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

jsonReq := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonReq,'Line[0].DetailType','SalesItemLineDetail');
CkJsonObject_UpdateNumber(jsonReq,'Line[0].Amount','100.0');
CkJsonObject_UpdateString(jsonReq,'Line[0].SalesItemLineDetail.ItemRef.name','Services');
CkJsonObject_UpdateString(jsonReq,'Line[0].SalesItemLineDetail.ItemRef.value','1');
CkJsonObject_UpdateString(jsonReq,'CustomerRef.value','1');

sbRequestBody := CkStringBuilder_Create();
CkJsonObject_EmitSb(jsonReq,sbRequestBody);

CkRest_AddHeader(rest,'Content-Type','application/json');
CkRest_AddHeader(rest,'Accept','application/json');
CkRest_putAllowHeaderFolding(rest,False);

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/v3/company/<realmID>/invoice',sbRequestBody,sbResponseBody);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

respStatusCode := CkRest_getResponseStatusCode(rest);

// Success is indicated by a 200 response status code.
Memo1.Lines.Add('response status code = ' + IntToStr(respStatusCode));

jsonResponse := CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResponse,sbResponseBody);
CkJsonObject_putEmitCompact(jsonResponse,False);
Memo1.Lines.Add(CkJsonObject__emit(jsonResponse));

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

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

InvoiceDocNumber := CkJsonObject__stringOf(jsonResponse,'Invoice.DocNumber');
InvoiceSyncToken := CkJsonObject__stringOf(jsonResponse,'Invoice.SyncToken');
InvoiceDomain := CkJsonObject__stringOf(jsonResponse,'Invoice.domain');
InvoiceBalance := CkJsonObject__stringOf(jsonResponse,'Invoice.Balance');
InvoiceBillAddrCity := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.City');
InvoiceBillAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line1');
InvoiceBillAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.PostalCode');
InvoiceBillAddrLat := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Lat');
InvoiceBillAddrLong := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Long');
InvoiceBillAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.CountrySubDivisionCode');
InvoiceBillAddrId := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Id');
InvoiceTxnDate := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnDate');
InvoiceTotalAmt := CkJsonObject__stringOf(jsonResponse,'Invoice.TotalAmt');
InvoiceCustomerRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerRef.name');
InvoiceCustomerRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerRef.value');
InvoiceShipAddrCity := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.City');
InvoiceShipAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line1');
InvoiceShipAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.PostalCode');
InvoiceShipAddrLat := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Lat');
InvoiceShipAddrLong := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Long');
InvoiceShipAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.CountrySubDivisionCode');
InvoiceShipAddrId := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Id');
InvoiceDueDate := CkJsonObject__stringOf(jsonResponse,'Invoice.DueDate');
InvoicePrintStatus := CkJsonObject__stringOf(jsonResponse,'Invoice.PrintStatus');
InvoiceDeposit := CkJsonObject_IntOf(jsonResponse,'Invoice.Deposit');
InvoiceSparse := CkJsonObject_BoolOf(jsonResponse,'Invoice.sparse');
InvoiceEmailStatus := CkJsonObject__stringOf(jsonResponse,'Invoice.EmailStatus');
InvoiceApplyTaxAfterDiscount := CkJsonObject_BoolOf(jsonResponse,'Invoice.ApplyTaxAfterDiscount');
InvoiceId := CkJsonObject__stringOf(jsonResponse,'Invoice.Id');
InvoiceTxnTaxDetailTotalTax := CkJsonObject_IntOf(jsonResponse,'Invoice.TxnTaxDetail.TotalTax');
InvoiceMetaDataCreateTime := CkJsonObject__stringOf(jsonResponse,'Invoice.MetaData.CreateTime');
InvoiceMetaDataLastUpdatedTime := CkJsonObject__stringOf(jsonResponse,'Invoice.MetaData.LastUpdatedTime');
time := CkJsonObject__stringOf(jsonResponse,'time');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.LinkedTxn');
while i < count_i do
  begin
    CkJsonObject_putI(jsonResponse,i);
    i := i + 1;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.Line');
while i < count_i do
  begin
    CkJsonObject_putI(jsonResponse,i);
    LineNum := CkJsonObject_IntOf(jsonResponse,'Invoice.Line[i].LineNum');
    Amount := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Amount');
    SalesItemLineDetailTaxCodeRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value');
    SalesItemLineDetailItemRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ItemRef.name');
    SalesItemLineDetailItemRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ItemRef.value');
    Id := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Id');
    DetailType := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].DetailType');
    i := i + 1;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.CustomField');
while i < count_i do
  begin
    CkJsonObject_putI(jsonResponse,i);
    DefinitionId := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].DefinitionId');
    invType := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].Type');
    Name := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].Name');
    i := i + 1;
  end;

CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);

end;