SQL Server
SQL Server
Shippo Create Customs Declaration
See more Shippo Examples
Demonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Implements the following CURL command:
-- curl https://api.goshippo.com/customs/declarations/ \
-- -H "Authorization: ShippoToken <API_TOKEN>" \
-- -H "Content-Type: application/json" \
-- -d '{
-- "contents_type": "MERCHANDISE",
-- "non_delivery_option": "RETURN",
-- "certify": true,
-- "certify_signer": "Simon Kreuz",
-- "incoterm": "DDU",
-- "items": [{
-- "description": "T-shirt",
-- "quantity": 20,
-- "net_weight": "5",
-- "mass_unit": "lb",
-- "value_amount": "200",
-- "value_currency": "USD",
-- "tariff_number": "",
-- "origin_country": "US"
-- }]
-- }'
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "contents_type": "MERCHANDISE",
-- "non_delivery_option": "RETURN",
-- "certify": true,
-- "certify_signer": "Simon Kreuz",
-- "incoterm": "DDU",
-- "items": [
-- {
-- "description": "T-shirt",
-- "quantity": 20,
-- "net_weight": "5",
-- "mass_unit": "lb",
-- "value_amount": "200",
-- "value_currency": "USD",
-- "tariff_number": "",
-- "origin_country": "US"
-- }
-- ]
-- }
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'contents_type', 'MERCHANDISE'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'non_delivery_option', 'RETURN'
EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'certify', 1
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'certify_signer', 'Simon Kreuz'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'incoterm', 'DDU'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].description', 'T-shirt'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].quantity', 20
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].net_weight', '5'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].mass_unit', 'lb'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].value_amount', '200'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].value_currency', 'USD'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].tariff_number', ''
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].origin_country', 'US'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'ShippoToken <API_TOKEN>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.goshippo.com/customs/declarations/', @json, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @sbResponseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "object_created": "2019-07-04T16:00:29.043Z",
-- "object_updated": "2019-07-04T16:00:29.043Z",
-- "object_id": "4a1e6ab7b1ba49ed9bc6cb1a8798e0fd",
-- "object_owner": "admin@chilkatsoft.com",
-- "object_state": "VALID",
-- "address_importer": null,
-- "certify_signer": "Simon Kreuz",
-- "certify": true,
-- "items": [
-- "4096c68693364b7ea0af72fb869ee861"
-- ],
-- "non_delivery_option": "RETURN",
-- "contents_type": "MERCHANDISE",
-- "contents_explanation": "",
-- "exporter_reference": "",
-- "importer_reference": "",
-- "invoice": "",
-- "commercial_invoice": false,
-- "license": "",
-- "certificate": "",
-- "notes": "",
-- "eel_pfc": "",
-- "aes_itn": "",
-- "disclaimer": "",
-- "incoterm": "DDU",
-- "metadata": "",
-- "test": true,
-- "duties_payor": null
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @strVal nvarchar(4000)
DECLARE @object_created nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @object_created OUT, 'object_created'
DECLARE @object_updated nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @object_updated OUT, 'object_updated'
DECLARE @object_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @object_id OUT, 'object_id'
DECLARE @object_owner nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @object_owner OUT, 'object_owner'
DECLARE @object_state nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @object_state OUT, 'object_state'
DECLARE @address_importer nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @address_importer OUT, 'address_importer'
DECLARE @certify_signer nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @certify_signer OUT, 'certify_signer'
DECLARE @certify int
EXEC sp_OAMethod @jResp, 'BoolOf', @certify OUT, 'certify'
DECLARE @non_delivery_option nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @non_delivery_option OUT, 'non_delivery_option'
DECLARE @contents_type nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @contents_type OUT, 'contents_type'
DECLARE @contents_explanation nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @contents_explanation OUT, 'contents_explanation'
DECLARE @exporter_reference nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @exporter_reference OUT, 'exporter_reference'
DECLARE @importer_reference nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @importer_reference OUT, 'importer_reference'
DECLARE @invoice nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @invoice OUT, 'invoice'
DECLARE @commercial_invoice int
EXEC sp_OAMethod @jResp, 'BoolOf', @commercial_invoice OUT, 'commercial_invoice'
DECLARE @license nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @license OUT, 'license'
DECLARE @certificate nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @certificate OUT, 'certificate'
DECLARE @notes nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @notes OUT, 'notes'
DECLARE @eel_pfc nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @eel_pfc OUT, 'eel_pfc'
DECLARE @aes_itn nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @aes_itn OUT, 'aes_itn'
DECLARE @disclaimer nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @disclaimer OUT, 'disclaimer'
DECLARE @incoterm nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @incoterm OUT, 'incoterm'
DECLARE @metadata nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @metadata OUT, 'metadata'
DECLARE @test int
EXEC sp_OAMethod @jResp, 'BoolOf', @test OUT, 'test'
DECLARE @duties_payor nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @duties_payor OUT, 'duties_payor'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'items'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'items[i]'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO