SQL Server 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'

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'test-api.service.hmrc.gov.uk', @port, @bTls, @bAutoReconnect
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer HMRC_ACCESS_TOKEN'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Accept', 'application/vnd.hmrc.1.0+json'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/organisations/vat/MY_HMRC_VRN/liabilities?from=2018-06-25&to=2019-02-17', @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Status Code = ' + @respStatusCode

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResponse OUT

    EXEC sp_OAMethod @jsonResponse, 'LoadSb', @success OUT, @sbResponseBody

    --  See the Online Tool for Generating JSON Parse Code
    DECLARE @i int

    DECLARE @count_i int

    DECLARE @taxPeriodFrom nvarchar(4000)

    DECLARE @taxPeriodTo nvarchar(4000)

    DECLARE @type nvarchar(4000)

    DECLARE @originalAmount nvarchar(4000)

    DECLARE @outstandingAmount nvarchar(4000)

    DECLARE @due nvarchar(4000)

    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'liabilities'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', @i
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @taxPeriodFrom OUT, 'liabilities[i].taxPeriod.from'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @taxPeriodTo OUT, 'liabilities[i].taxPeriod.to'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'liabilities[i].type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @originalAmount OUT, 'liabilities[i].originalAmount'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @outstandingAmount OUT, 'liabilities[i].outstandingAmount'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @due OUT, 'liabilities[i].due'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

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