Delphi DLL EasyPost: Add Shipments to a Batch

Back to Index

Add shipments to a batch.

Documentation: https://www.easypost.com/docs/api#add-shipments-to-a-batch

CURL Command

curl -X POST https://api.easypost.com/v2/batches/batch_.../add_shipments \
  -u <YOUR_TEST/PRODUCTION_API_KEY>: \
  -d 'batch[shipments][0][id]=shp_...' \
  -d 'batch[shipments][1][id]=shp_...'

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
strResponseBody: PWideChar;
jsonResponse: HCkJsonObject;
id: PWideChar;
label_url: Boolean;
mode: PWideChar;
num_shipments: Integer;
object: PWideChar;
reference: Boolean;
scan_form: Boolean;
state: PWideChar;
statusCreated: Integer;
statusQueued_for_purchase: Integer;
statusCreation_failed: Integer;
statusPostage_purchased: Integer;
statusPostage_purchase_failed: Integer;
created_at: PWideChar;
updated_at: PWideChar;
i: Integer;
count_i: Integer;
intVal: Integer;

begin
rest := CkRest_Create();

//  URL: https://api.easypost.com/v2/batches/batch_.../add_shipments
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'api.easypost.com',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_SetAuthBasic(rest,'<YOUR_TEST/PRODUCTION_API_KEY>','');

CkRest_AddQueryParam(rest,'batch[shipments][0][id]','shp_...');
CkRest_AddQueryParam(rest,'batch[shipments][1][id]','shp_...');

strResponseBody := CkRest__fullRequestFormUrlEncoded(rest,'POST','/v2/batches/batch_.../add_shipments');
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,strResponseBody);

id := CkJsonObject__stringOf(jsonResponse,'id');
label_url := CkJsonObject_IsNullOf(jsonResponse,'label_url');
mode := CkJsonObject__stringOf(jsonResponse,'mode');
num_shipments := CkJsonObject_IntOf(jsonResponse,'num_shipments');
object := CkJsonObject__stringOf(jsonResponse,'object');
reference := CkJsonObject_IsNullOf(jsonResponse,'reference');
scan_form := CkJsonObject_IsNullOf(jsonResponse,'scan_form');
state := CkJsonObject__stringOf(jsonResponse,'state');
statusCreated := CkJsonObject_IntOf(jsonResponse,'status.created');
statusQueued_for_purchase := CkJsonObject_IntOf(jsonResponse,'status.queued_for_purchase');
statusCreation_failed := CkJsonObject_IntOf(jsonResponse,'status.creation_failed');
statusPostage_purchased := CkJsonObject_IntOf(jsonResponse,'status.postage_purchased');
statusPostage_purchase_failed := CkJsonObject_IntOf(jsonResponse,'status.postage_purchase_failed');
created_at := CkJsonObject__stringOf(jsonResponse,'created_at');
updated_at := CkJsonObject__stringOf(jsonResponse,'updated_at');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'shipments');
while i < count_i do
  begin
CkJsonObject_putI(jsonResponse,i);
    intVal := CkJsonObject_IntOf(jsonResponse,'shipments[i]');
    i := i + 1;
  end;

CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

{
  "id": "batch_...",
  "label_url": null,
  "mode": "test",
  "num_shipments": 5,
  "object": "Batch",
  "reference": null,
  "scan_form": null,
  "shipments": [
    ....
  ],
  "state": "creating",
  "status": {
    "created": 0,
    "queued_for_purchase": 0,
    "creation_failed": 0,
    "postage_purchased": 5,
    "postage_purchase_failed": 0
  },
  "label_url": null,
  "created_at": "2014-07-22T07:34:39Z",
  "updated_at": "2014-07-22T07:34:39Z"
}