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

PureBasic Example

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns/A001
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"test-api.service.hmrc.gov.uk",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::ckAddHeader(rest,"Authorization","Bearer HMRC_ACCESS_TOKEN")
    CkRest::ckAddHeader(rest,"Accept","application/vnd.hmrc.1.0+json")

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/organisations/vat/MY_HMRC_VRN/returns/A001",sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)

    ;  See the Online Tool for Generating JSON Parse Code
    periodKey.s
    vatDueSales.s
    vatDueAcquisitions.s
    totalVatDue.s
    vatReclaimedCurrPeriod.s
    netVatDue.s
    totalValueSalesExVAT.i
    totalValuePurchasesExVAT.i
    totalValueGoodsSuppliedExVAT.i
    totalAcquisitionsExVAT.i

    periodKey = CkJsonObject::ckStringOf(jsonResponse,"periodKey")
    vatDueSales = CkJsonObject::ckStringOf(jsonResponse,"vatDueSales")
    vatDueAcquisitions = CkJsonObject::ckStringOf(jsonResponse,"vatDueAcquisitions")
    totalVatDue = CkJsonObject::ckStringOf(jsonResponse,"totalVatDue")
    vatReclaimedCurrPeriod = CkJsonObject::ckStringOf(jsonResponse,"vatReclaimedCurrPeriod")
    netVatDue = CkJsonObject::ckStringOf(jsonResponse,"netVatDue")
    totalValueSalesExVAT = CkJsonObject::ckIntOf(jsonResponse,"totalValueSalesExVAT")
    totalValuePurchasesExVAT = CkJsonObject::ckIntOf(jsonResponse,"totalValuePurchasesExVAT")
    totalValueGoodsSuppliedExVAT = CkJsonObject::ckIntOf(jsonResponse,"totalValueGoodsSuppliedExVAT")
    totalAcquisitionsExVAT = CkJsonObject::ckIntOf(jsonResponse,"totalAcquisitionsExVAT")


    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

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
}