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

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

    --  See the Online Tool for Generating JSON Creation Code
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'periodKey', 'A001'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'vatDueSales', '105.50'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'vatDueAcquisitions', '-100.45'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'totalVatDue', '5.05'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'vatReclaimedCurrPeriod', '105.15'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'netVatDue', '100.10'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'totalValueSalesExVAT', '300'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'totalValuePurchasesExVAT', '300'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'totalValueGoodsSuppliedExVAT', '3000'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'totalAcquisitionsExVAT', '3000'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'finalised', 1

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'
    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 @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody
    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/organisations/vat/MY_HMRC_VRN/returns', @sbRequestBody, @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        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 @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        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 @processingDate nvarchar(4000)

    DECLARE @paymentIndicator nvarchar(4000)

    DECLARE @formBundleNumber nvarchar(4000)

    EXEC sp_OAMethod @jsonResponse, 'StringOf', @processingDate OUT, 'processingDate'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @paymentIndicator OUT, 'paymentIndicator'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @formBundleNumber OUT, 'formBundleNumber'

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


END
GO

Sample JSON Response Body

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