Delphi DLL HMRC VAT MTD: Submit VAT return for period

Back to Index

Submit VAT return for period

Documentation: https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-api/1.0#_submit-vat-return-for-period_post_accordion

CURL Command

curl -X POST  https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns \
-H 'Authorization: Bearer HMRC_ACCESS_TOKEN' \
-H 'Accept: application/vnd.hmrc.1.0+json' \
-H 'Content-Type: application/json' \
-d '{
  "periodKey": "A001",
  "vatDueSales": 105.50,
  "vatDueAcquisitions": -100.45,
  "totalVatDue": 5.05,
  "vatReclaimedCurrPeriod": 105.15,
  "netVatDue": 100.10,
  "totalValueSalesExVAT": 300,
  "totalValuePurchasesExVAT": 300,
  "totalValueGoodsSuppliedExVAT": 3000,
  "totalAcquisitionsExVAT": 3000,
  "finalised": true
}'

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
json: HCkJsonObject;
sbRequestBody: HCkStringBuilder;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
processingDate: PWideChar;
paymentIndicator: PWideChar;
formBundleNumber: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
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;

//  See the Online Tool for Generating JSON Creation Code
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'periodKey','A001');
CkJsonObject_UpdateNumber(json,'vatDueSales','105.50');
CkJsonObject_UpdateNumber(json,'vatDueAcquisitions','-100.45');
CkJsonObject_UpdateNumber(json,'totalVatDue','5.05');
CkJsonObject_UpdateNumber(json,'vatReclaimedCurrPeriod','105.15');
CkJsonObject_UpdateNumber(json,'netVatDue','100.10');
CkJsonObject_UpdateNumber(json,'totalValueSalesExVAT','300');
CkJsonObject_UpdateNumber(json,'totalValuePurchasesExVAT','300');
CkJsonObject_UpdateNumber(json,'totalValueGoodsSuppliedExVAT','3000');
CkJsonObject_UpdateNumber(json,'totalAcquisitionsExVAT','3000');
CkJsonObject_UpdateBool(json,'finalised',True);

CkRest_AddHeader(rest,'Content-Type','application/json');
CkRest_AddHeader(rest,'Authorization','Bearer HMRC_ACCESS_TOKEN');
CkRest_AddHeader(rest,'Accept','application/vnd.hmrc.1.0+json');

sbRequestBody := CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sbRequestBody);
sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/organisations/vat/MY_HMRC_VRN/returns',sbRequestBody,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

processingDate := CkJsonObject__stringOf(jsonResponse,'processingDate');
paymentIndicator := CkJsonObject__stringOf(jsonResponse,'paymentIndicator');
formBundleNumber := CkJsonObject__stringOf(jsonResponse,'formBundleNumber');

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

Sample JSON Response Body

{
  "processingDate": "2019-02-18T14:37:56.561Z",
  "paymentIndicator": "BANK",
  "formBundleNumber": "948551092841"
}