Sample code for 30+ languages & platforms
Dart

Quickbooks Delete an Invoice

See more QuickBooks Examples

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final jsonToken = CkJsonObject();
  jsonToken.loadFile('qa_data/tokens/qb-access-token.json');

  final rest = CkRest();

  // Connect to the REST server.
  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  rest.connect('sandbox-quickbooks.api.intuit.com', port, bTls, bAutoReconnect);

  final sbAuth = CkStringBuilder();
  sbAuth.append('Bearer ');
  sbAuth.append(jsonToken.stringOf('access_token'));
  rest.authorization = sbAuth.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.
  // --------------------------------------------------------------------------

  // Create the following JSON:

  // {
  //   "SyncToken": "3",
  //   "Id": "33"
  // }
  // 
  // Use the this online tool to generate the code from sample JSON: 
  // Generate Code to Create JSON

  final jsonReq = CkJsonObject();
  jsonReq.updateString('SyncToken', '3');
  jsonReq.updateString('Id', '33');

  final sbRequestBody = CkStringBuilder();
  jsonReq.emitSb(sbRequestBody);

  rest.addHeader('Content-Type', 'application/json');
  rest.addHeader('Accept', 'application/json');
  rest.allowHeaderFolding = false;

  final sbResponseBody = CkStringBuilder();
  try {
    rest.fullRequestSb('POST', '/v3/company/<realmID>/invoice?operation=delete', sbRequestBody, sbResponseBody);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final respStatusCode = rest.responseStatusCode;

  // Success is indicated by a 200 response status code.
  print('response status code = $respStatusCode');

  final jsonResponse = CkJsonObject();
  jsonResponse.loadSb(sbResponseBody);
  jsonResponse.emitCompact = false;
  print(jsonResponse.emit());

  if (rest.responseStatusCode != 200) {
    print('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": {
  //     "status": "Deleted",
  //     "domain": "QBO",
  //     "Id": "33"
  //   },
  //   "time": "2013-03-15T00:18:15.322-07:00"
  // }
  // 

  final invoiceStatus = jsonResponse.stringOf('Invoice.status');
  final invoiceDomain = jsonResponse.stringOf('Invoice.domain');
  final invoiceId = jsonResponse.stringOf('Invoice.Id');
  final time = jsonResponse.stringOf('time');
}