DataFlex 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'

DataFlex Example

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Integer iRespStatusCode
    Handle hoJsonResponse
    Integer i
    Integer iCount_i
    String sTaxPeriodFrom
    String sTaxPeriodTo
    String sType
    String sOriginalAmount
    String sOutstandingAmount
    String sDue
    String sTemp1
    Integer iTemp1

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    //  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17
    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "test-api.service.hmrc.gov.uk" iPort iBTls iBAutoReconnect To iSuccess
    If (iSuccess <> True) Begin
        Get ComConnectFailReason Of hoRest To iTemp1
        Showln "ConnectFailReason: " iTemp1
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "Authorization" "Bearer HMRC_ACCESS_TOKEN" To iSuccess
    Get ComAddHeader Of hoRest "Accept" "application/vnd.hmrc.1.0+json" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComFullRequestNoBodySb Of hoRest "GET" "/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17" vSbResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Status Code = " iRespStatusCode
        Showln "Response Header:"
        Get ComResponseHeader Of hoRest To sTemp1
        Showln sTemp1
        Showln "Response Body:"
        Get ComGetAsString Of hoSbResponseBody To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
    If (Not(IsComObjectCreated(hoJsonResponse))) Begin
        Send CreateComObject of hoJsonResponse
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJsonResponse vSbResponseBody To iSuccess

    //  See the Online Tool for Generating JSON Parse Code

    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "liabilities" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "liabilities[i].taxPeriod.from" To sTaxPeriodFrom
        Get ComStringOf Of hoJsonResponse "liabilities[i].taxPeriod.to" To sTaxPeriodTo
        Get ComStringOf Of hoJsonResponse "liabilities[i].type" To sType
        Get ComStringOf Of hoJsonResponse "liabilities[i].originalAmount" To sOriginalAmount
        Get ComStringOf Of hoJsonResponse "liabilities[i].outstandingAmount" To sOutstandingAmount
        Get ComStringOf Of hoJsonResponse "liabilities[i].due" To sDue
        Move i + 1 To i
    Loop



End_Procedure

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"
    }
  ]
}