Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

QuickBooks - Create an Account

See more QuickBooks Examples

Demonstrates how to send an JSON request to create a QuickBooks account.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringBuilder,
  Chilkat.Rest,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  jsonToken: TJsonObject;
  rest: TRest;
  bAutoReconnect: Boolean;
  sbAuth: TStringBuilder;
  jsonRequest: TJsonObject;
  requestBody: string;
  sbPath: TStringBuilder;
  responseBody: string;
  jsonResponse: TJsonObject;

begin
  success := False;

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

  //  First get our previously obtained OAuth2 access token.
  jsonToken := TJsonObject.Create;
  success := jsonToken.LoadFile('qa_data/tokens/qb-access-token.json');

  rest := TRest.Create;

  bAutoReconnect := True;
  success := rest.Connect('sandbox-quickbooks.api.intuit.com',443,True,bAutoReconnect);
  if (success <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  sbAuth := TStringBuilder.Create;
  sbAuth.Append('Bearer ');
  sbAuth.Append(jsonToken.StringOf('access_token'));
  rest.Authorization := sbAuth.GetAsString();

  jsonRequest := TJsonObject.Create;
  jsonRequest.AppendString('AccountType','Credit Card');
  jsonRequest.AppendString('Name','Banana Republic');
  requestBody := jsonRequest.Emit();

  //  "123146096291789" is the company ID.
  sbPath := TStringBuilder.Create;
  sbPath.Append('/v3/company/123146096291789/account?minorversion=45');

  rest.AddHeader('Content-Type','application/json');
  rest.AddHeader('Accept','application/json');
  rest.AllowHeaderFolding := False;

  responseBody := rest.FullRequestString('POST',sbPath.GetAsString(),requestBody);
  if (rest.LastMethodSuccess <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  //  We should expect a 200 response if successful.
  if (rest.ResponseStatusCode <> 200) then
    begin
      WriteLn('Request Header: ');
      WriteLn(rest.LastRequestHeader);
      WriteLn('----');
      WriteLn('Response StatusCode = ' + rest.ResponseStatusCode);
      WriteLn('Response StatusLine: ' + rest.ResponseStatusText);
      WriteLn('Response Header:');
      WriteLn(rest.ResponseHeader);
      WriteLn(responseBody);
      Exit;
    end;

  jsonResponse := TJsonObject.Create;
  jsonResponse.Load(responseBody);
  jsonResponse.EmitCompact := False;
  WriteLn(jsonResponse.Emit());
  WriteLn('Success.');

  //  A sample JSON response:

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

  //  {
  //    "Account": {
  //      "Name": "Banana Republic",
  //      "SubAccount": false,
  //      "FullyQualifiedName": "Banana Republic",
  //      "Active": true,
  //      "Classification": "Liability",
  //      "AccountType": "Credit Card",
  //      "AccountSubType": "CreditCard",
  //      "CurrentBalance": 0,
  //      "CurrentBalanceWithSubAccounts": 0,
  //      "CurrencyRef": {
  //        "value": "USD",
  //        "name": "United States Dollar"
  //      },
  //      "domain": "QBO",
  //      "sparse": false,
  //      "Id": "97",
  //      "SyncToken": "0",
  //      "MetaData": {
  //        "CreateTime": "2016-10-25T05:07:12-07:00",
  //        "LastUpdatedTime": "2016-10-25T05:07:12-07:00"
  //      }
  //    },
  //    "time": "2016-10-25T05:07:11.714-07:00"
  //  }


  jsonToken.Free;
  rest.Free;
  sbAuth.Free;
  jsonRequest.Free;
  sbPath.Free;
  jsonResponse.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.