SQL Server HMRC VAT MTD: View VAT Return

Back to Index

Retrieve a previously submitted VAT return. This example retrieves the VAT return with period key "A001".

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

CURL Command

curl -X GET  https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns/A001 \
-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/returns/A001
    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/returns/A001', @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 @periodKey nvarchar(4000)

    DECLARE @vatDueSales nvarchar(4000)

    DECLARE @vatDueAcquisitions nvarchar(4000)

    DECLARE @totalVatDue nvarchar(4000)

    DECLARE @vatReclaimedCurrPeriod nvarchar(4000)

    DECLARE @netVatDue nvarchar(4000)

    DECLARE @totalValueSalesExVAT int

    DECLARE @totalValuePurchasesExVAT int

    DECLARE @totalValueGoodsSuppliedExVAT int

    DECLARE @totalAcquisitionsExVAT int

    EXEC sp_OAMethod @jsonResponse, 'StringOf', @periodKey OUT, 'periodKey'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @vatDueSales OUT, 'vatDueSales'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @vatDueAcquisitions OUT, 'vatDueAcquisitions'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @totalVatDue OUT, 'totalVatDue'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @vatReclaimedCurrPeriod OUT, 'vatReclaimedCurrPeriod'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @netVatDue OUT, 'netVatDue'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @totalValueSalesExVAT OUT, 'totalValueSalesExVAT'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @totalValuePurchasesExVAT OUT, 'totalValuePurchasesExVAT'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @totalValueGoodsSuppliedExVAT OUT, 'totalValueGoodsSuppliedExVAT'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @totalAcquisitionsExVAT OUT, 'totalAcquisitionsExVAT'

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


END
GO

Sample JSON Response Body

{
  "periodKey": "A001",
  "vatDueSales": 105.5,
  "vatDueAcquisitions": -100.45,
  "totalVatDue": 5.05,
  "vatReclaimedCurrPeriod": 105.15,
  "netVatDue": 100.1,
  "totalValueSalesExVAT": 300,
  "totalValuePurchasesExVAT": 300,
  "totalValueGoodsSuppliedExVAT": 3000,
  "totalAcquisitionsExVAT": 3000
}