Delphi DLL EasyPost: Create a Batch

Back to Index

Create an EasyPost batch.

Documentation: https://www.easypost.com/docs/api#create-a-batch

CURL Command

curl -X POST https://api.easypost.com/v2/batches \
  -u <YOUR_TEST/PRODUCTION_API_KEY>: \
  -d "batch[shipments][0][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;
batch_status: PWideChar;
batch_message: Boolean;

begin
rest := CkRest_Create();

//  URL: https://api.easypost.com/v2/batches
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_...');

strResponseBody := CkRest__fullRequestFormUrlEncoded(rest,'POST','/v2/batches');
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);
    id := CkJsonObject__stringOf(jsonResponse,'shipments[i].id');
    batch_status := CkJsonObject__stringOf(jsonResponse,'shipments[i].batch_status');
    batch_message := CkJsonObject_IsNullOf(jsonResponse,'shipments[i].batch_message');
    reference := CkJsonObject_IsNullOf(jsonResponse,'shipments[i].reference');
    i := i + 1;
  end;

CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

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