Delphi DLL HMRC VAT MTD: Retrieve VAT Liabilities

Back to Index

Retrieve VAT liabilities

Documentation: https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-api/1.0#_retrieve-vat-liabilities_get_accordion

CURL Command

curl -X GET  https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17 \
-H 'Authorization: Bearer HMRC_ACCESS_TOKEN' \
-H 'Accept: application/vnd.hmrc.1.0+json'

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
i: Integer;
count_i: Integer;
taxPeriodFrom: PWideChar;
taxPeriodTo: PWideChar;
type: PWideChar;
originalAmount: PWideChar;
outstandingAmount: PWideChar;
due: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'test-api.service.hmrc.gov.uk',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_AddHeader(rest,'Authorization','Bearer HMRC_ACCESS_TOKEN');
CkRest_AddHeader(rest,'Accept','application/vnd.hmrc.1.0+json');

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17',sbResponseBody);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
respStatusCode := CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkRest__responseHeader(rest));
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResponse,sbResponseBody);

//  See the Online Tool for Generating JSON Parse Code

i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'liabilities');
while i < count_i do
  begin
    CkJsonObject_putI(jsonResponse,i);
    taxPeriodFrom := CkJsonObject__stringOf(jsonResponse,'liabilities[i].taxPeriod.from');
    taxPeriodTo := CkJsonObject__stringOf(jsonResponse,'liabilities[i].taxPeriod.to');
    type := CkJsonObject__stringOf(jsonResponse,'liabilities[i].type');
    originalAmount := CkJsonObject__stringOf(jsonResponse,'liabilities[i].originalAmount');
    outstandingAmount := CkJsonObject__stringOf(jsonResponse,'liabilities[i].outstandingAmount');
    due := CkJsonObject__stringOf(jsonResponse,'liabilities[i].due');
    i := i + 1;
  end;

CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

{
  "liabilities": [
    {
      "taxPeriod": {
        "from": "2018-04-06",
        "to": "2018-07-06"
      },
      "type": "VAT ...",
      "originalAmount": 6000.3,
      "outstandingAmount": 100.51,
      "due": "2018-07-06"
    }
  ]
}