SQL Server
SQL Server
Amazon SP-API Get Orders
See more Amazon SP-API Examples
Demonstrates Amazon SP-API Sellers API -- get orders. Returns orders created or updated during the time frame indicated by the specified parameters.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 requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- IMPORTANT:
-- You'll need to make changes to use values specific to your own region and account.
--
-- Here is a list of values you'll need to change or provide:
--
-- 1) The access key. Change AWS_ACCESS_KEY to your actual access key.
-- 2) The secret key. Change AWS_SECRET_KEY to your actual access key.
-- 3) Your region. Change "ew-west-1" to your region.
-- 4) The domain to which you connect. Change sandbox.sellingpartnerapi-eu.amazon.com to non-sandbox by removing the "sandbox."
-- and change the "-eu" part of the domain to your region. See https://developer-docs.amazon.com/sp-api/docs/sp-api-endpoints
-- 5) Use the restricted data token (RDT) obtained from your code that previously obtained the RDT.
-- If you saved the RDT to a file, update the relative file path "qa_data/tokens/sp_api_rdt_token.json" to your actual file path.
-- 6) Update your Marketplace ID(s), such as ATVPDKIKX0DER
-- 7) If using the non-sandbox, change the CreatedAfter from "TEST_CASE_200" to an actual date, such as "2022-12-25"
DECLARE @authAws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @authAws OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'
EXEC sp_OASetProperty @authAws, 'ServiceName', 'execute-api'
-- Use the region that is correct for your needs.
EXEC sp_OASetProperty @authAws, 'Region', 'eu-west-1'
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
DECLARE @bTls int
SELECT @bTls = 1
DECLARE @port int
SELECT @port = 443
DECLARE @bAutoReconnect int
SELECT @bAutoReconnect = 1
-- Make sure to use the correct domain.
-- This example is using the sandbox
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'sandbox.sellingpartnerapi-eu.amazon.com', @port, @bTls, @bAutoReconnect
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws
-- Here's the example to get the RDT access token: Get SP-API Restricted Data Token
-- Load the previously obtained RDT access token.
-- See Fetch SP-API RDT Access Token
DECLARE @jsonToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT
EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/sp_api_rdt_token.json'
IF @success = 0
BEGIN
PRINT 'Failed to load RDT access token.'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @jsonToken
RETURN
END
-- Add the x-amz-access-token request header.
DECLARE @rdt_token nvarchar(4000)
EXEC sp_OAMethod @jsonToken, 'StringOf', @rdt_token OUT, 'restrictedDataToken'
EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-amz-access-token', @rdt_token
-- Indicate the marketplace IDs. Use the marketplace ID for your needs. See https://developer-docs.amazon.com/sp-api/docs/marketplace-ids
EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
-- When using the sandbox, use these params literally and exactly has shown here:
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'MarketplaceIds', 'ATVPDKIKX0DER'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'CreatedAfter', 'TEST_CASE_200'
DECLARE @sbResponse int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponse OUT
DECLARE @uri nvarchar(4000)
SELECT @uri = '/orders/v0/orders'
EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', @uri, @sbResponse
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @sbResponse
RETURN
END
-- Examine the response status.
DECLARE @statusCode int
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @statusCode OUT
IF @statusCode <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
PRINT 'Response status text: ' + @sTmp0
PRINT 'Response body: '
EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @sbResponse
RETURN
END
EXEC sp_OAMethod @sbResponse, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
-- If successful, gets a JSON response such as the following:
-- {
-- "payload": {
-- "CreatedBefore": "1.569521782042E9",
-- "Orders": [
-- {
-- "AmazonOrderId": "902-1845936-5435065",
-- "PurchaseDate": "1970-01-19T03:58:30Z",
-- "LastUpdateDate": "1970-01-19T03:58:32Z",
-- "OrderStatus": "Unshipped",
-- "FulfillmentChannel": "MFN",
-- "SalesChannel": "Amazon.com",
-- "ShipServiceLevel": "Std US D2D Dom",
-- "OrderTotal": {
-- "CurrencyCode": "USD",
-- "Amount": "11.01"
-- },
-- "NumberOfItemsShipped": 0,
-- "NumberOfItemsUnshipped": 1,
-- "PaymentMethod": "Other",
-- "PaymentMethodDetails": [
-- "Standard"
-- ],
-- "IsReplacementOrder": false,
-- "MarketplaceId": "ATVPDKIKX0DER",
-- "ShipmentServiceLevelCategory": "Standard",
-- "OrderType": "StandardOrder",
-- "EarliestShipDate": "1970-01-19T03:59:27Z",
-- "LatestShipDate": "1970-01-19T04:05:13Z",
-- "EarliestDeliveryDate": "1970-01-19T04:06:39Z",
-- "LatestDeliveryDate": "1970-01-19T04:15:17Z",
-- "IsBusinessOrder": false,
-- "IsPrime": false,
-- "IsGlobalExpressEnabled": false,
-- "IsPremiumOrder": false,
-- "IsSoldByAB": false,
-- "IsIBA": false,
-- "DefaultShipFromLocationAddress": {
-- "Name": "MFNIntegrationTestMerchant",
-- "AddressLine1": "2201 WESTLAKE AVE",
-- "City": "SEATTLE",
-- "StateOrRegion": "WA",
-- "PostalCode": "98121-2778",
-- "CountryCode": "US",
-- "Phone": "+1 480-386-0930 ext. 73824",
-- "AddressType": "Commercial"
-- },
-- "FulfillmentInstruction": {
-- "FulfillmentSupplySourceId": "sampleSupplySourceId"
-- },
-- "IsISPU": false,
-- "IsAccessPointOrder": false,
-- "AutomatedShippingSettings": {
-- "HasAutomatedShippingSettings": false
-- },
-- "EasyShipShipmentStatus": "PendingPickUp",
-- "ElectronicInvoiceStatus": "NotRequired"
-- },
-- {
-- "AmazonOrderId": "902-8745147-1934268",
-- "PurchaseDate": "1970-01-19T03:58:30Z",
-- "LastUpdateDate": "1970-01-19T03:58:32Z",
-- "OrderStatus": "Unshipped",
-- "FulfillmentChannel": "MFN",
-- "SalesChannel": "Amazon.com",
-- "ShipServiceLevel": "Std US D2D Dom",
-- "OrderTotal": {
-- "CurrencyCode": "USD",
-- "Amount": "11.01"
-- },
-- "NumberOfItemsShipped": 0,
-- "NumberOfItemsUnshipped": 1,
-- "PaymentMethod": "Other",
-- "PaymentMethodDetails": [
-- "Standard"
-- ],
-- "IsReplacementOrder": false,
-- "MarketplaceId": "ATVPDKIKX0DER",
-- "ShipmentServiceLevelCategory": "Standard",
-- "OrderType": "StandardOrder",
-- "EarliestShipDate": "1970-01-19T03:59:27Z",
-- "LatestShipDate": "1970-01-19T04:05:13Z",
-- "EarliestDeliveryDate": "1970-01-19T04:06:39Z",
-- "LatestDeliveryDate": "1970-01-19T04:15:17Z",
-- "IsBusinessOrder": false,
-- "IsPrime": false,
-- "IsAccessPointOrder": false,
-- "IsGlobalExpressEnabled": false,
-- "IsPremiumOrder": false,
-- "IsSoldByAB": false,
-- "IsIBA": false,
-- "EasyShipShipmentStatus": "PendingPickUp",
-- "ElectronicInvoiceStatus": "NotRequired"
-- }
-- ]
-- }
-- }
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sbResponse
DECLARE @AmazonOrderId nvarchar(4000)
DECLARE @PurchaseDate nvarchar(4000)
DECLARE @LastUpdateDate nvarchar(4000)
DECLARE @OrderStatus nvarchar(4000)
DECLARE @FulfillmentChannel nvarchar(4000)
DECLARE @SalesChannel nvarchar(4000)
DECLARE @ShipServiceLevel nvarchar(4000)
DECLARE @CurrencyCode nvarchar(4000)
DECLARE @Amount nvarchar(4000)
DECLARE @NumberOfItemsShipped int
DECLARE @NumberOfItemsUnshipped int
DECLARE @PaymentMethod nvarchar(4000)
DECLARE @IsReplacementOrder int
DECLARE @MarketplaceId nvarchar(4000)
DECLARE @ShipmentServiceLevelCategory nvarchar(4000)
DECLARE @OrderType nvarchar(4000)
DECLARE @EarliestShipDate nvarchar(4000)
DECLARE @LatestShipDate nvarchar(4000)
DECLARE @EarliestDeliveryDate nvarchar(4000)
DECLARE @LatestDeliveryDate nvarchar(4000)
DECLARE @IsBusinessOrder int
DECLARE @IsPrime int
DECLARE @IsGlobalExpressEnabled int
DECLARE @IsPremiumOrder int
DECLARE @IsSoldByAB int
DECLARE @IsIBA int
DECLARE @Name nvarchar(4000)
DECLARE @AddressLine1 nvarchar(4000)
DECLARE @City nvarchar(4000)
DECLARE @StateOrRegion nvarchar(4000)
DECLARE @PostalCode nvarchar(4000)
DECLARE @CountryCode nvarchar(4000)
DECLARE @Phone nvarchar(4000)
DECLARE @AddressType nvarchar(4000)
DECLARE @FulfillmentSupplySourceId nvarchar(4000)
DECLARE @IsISPU int
DECLARE @IsAccessPointOrder int
DECLARE @HasAutomatedShippingSettings int
DECLARE @EasyShipShipmentStatus nvarchar(4000)
DECLARE @ElectronicInvoiceStatus nvarchar(4000)
DECLARE @j int
DECLARE @count_j int
DECLARE @strVal nvarchar(4000)
DECLARE @CreatedBefore nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @CreatedBefore OUT, 'payload.CreatedBefore'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'payload.Orders'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @json, 'I', @i
EXEC sp_OAMethod @json, 'StringOf', @AmazonOrderId OUT, 'payload.Orders[i].AmazonOrderId'
EXEC sp_OAMethod @json, 'StringOf', @PurchaseDate OUT, 'payload.Orders[i].PurchaseDate'
EXEC sp_OAMethod @json, 'StringOf', @LastUpdateDate OUT, 'payload.Orders[i].LastUpdateDate'
EXEC sp_OAMethod @json, 'StringOf', @OrderStatus OUT, 'payload.Orders[i].OrderStatus'
EXEC sp_OAMethod @json, 'StringOf', @FulfillmentChannel OUT, 'payload.Orders[i].FulfillmentChannel'
EXEC sp_OAMethod @json, 'StringOf', @SalesChannel OUT, 'payload.Orders[i].SalesChannel'
EXEC sp_OAMethod @json, 'StringOf', @ShipServiceLevel OUT, 'payload.Orders[i].ShipServiceLevel'
EXEC sp_OAMethod @json, 'StringOf', @CurrencyCode OUT, 'payload.Orders[i].OrderTotal.CurrencyCode'
EXEC sp_OAMethod @json, 'StringOf', @Amount OUT, 'payload.Orders[i].OrderTotal.Amount'
EXEC sp_OAMethod @json, 'IntOf', @NumberOfItemsShipped OUT, 'payload.Orders[i].NumberOfItemsShipped'
EXEC sp_OAMethod @json, 'IntOf', @NumberOfItemsUnshipped OUT, 'payload.Orders[i].NumberOfItemsUnshipped'
EXEC sp_OAMethod @json, 'StringOf', @PaymentMethod OUT, 'payload.Orders[i].PaymentMethod'
EXEC sp_OAMethod @json, 'BoolOf', @IsReplacementOrder OUT, 'payload.Orders[i].IsReplacementOrder'
EXEC sp_OAMethod @json, 'StringOf', @MarketplaceId OUT, 'payload.Orders[i].MarketplaceId'
EXEC sp_OAMethod @json, 'StringOf', @ShipmentServiceLevelCategory OUT, 'payload.Orders[i].ShipmentServiceLevelCategory'
EXEC sp_OAMethod @json, 'StringOf', @OrderType OUT, 'payload.Orders[i].OrderType'
EXEC sp_OAMethod @json, 'StringOf', @EarliestShipDate OUT, 'payload.Orders[i].EarliestShipDate'
EXEC sp_OAMethod @json, 'StringOf', @LatestShipDate OUT, 'payload.Orders[i].LatestShipDate'
EXEC sp_OAMethod @json, 'StringOf', @EarliestDeliveryDate OUT, 'payload.Orders[i].EarliestDeliveryDate'
EXEC sp_OAMethod @json, 'StringOf', @LatestDeliveryDate OUT, 'payload.Orders[i].LatestDeliveryDate'
EXEC sp_OAMethod @json, 'BoolOf', @IsBusinessOrder OUT, 'payload.Orders[i].IsBusinessOrder'
EXEC sp_OAMethod @json, 'BoolOf', @IsPrime OUT, 'payload.Orders[i].IsPrime'
EXEC sp_OAMethod @json, 'BoolOf', @IsGlobalExpressEnabled OUT, 'payload.Orders[i].IsGlobalExpressEnabled'
EXEC sp_OAMethod @json, 'BoolOf', @IsPremiumOrder OUT, 'payload.Orders[i].IsPremiumOrder'
EXEC sp_OAMethod @json, 'BoolOf', @IsSoldByAB OUT, 'payload.Orders[i].IsSoldByAB'
EXEC sp_OAMethod @json, 'BoolOf', @IsIBA OUT, 'payload.Orders[i].IsIBA'
EXEC sp_OAMethod @json, 'StringOf', @Name OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.Name'
EXEC sp_OAMethod @json, 'StringOf', @AddressLine1 OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.AddressLine1'
EXEC sp_OAMethod @json, 'StringOf', @City OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.City'
EXEC sp_OAMethod @json, 'StringOf', @StateOrRegion OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.StateOrRegion'
EXEC sp_OAMethod @json, 'StringOf', @PostalCode OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.PostalCode'
EXEC sp_OAMethod @json, 'StringOf', @CountryCode OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.CountryCode'
EXEC sp_OAMethod @json, 'StringOf', @Phone OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.Phone'
EXEC sp_OAMethod @json, 'StringOf', @AddressType OUT, 'payload.Orders[i].DefaultShipFromLocationAddress.AddressType'
EXEC sp_OAMethod @json, 'StringOf', @FulfillmentSupplySourceId OUT, 'payload.Orders[i].FulfillmentInstruction.FulfillmentSupplySourceId'
EXEC sp_OAMethod @json, 'BoolOf', @IsISPU OUT, 'payload.Orders[i].IsISPU'
EXEC sp_OAMethod @json, 'BoolOf', @IsAccessPointOrder OUT, 'payload.Orders[i].IsAccessPointOrder'
EXEC sp_OAMethod @json, 'BoolOf', @HasAutomatedShippingSettings OUT, 'payload.Orders[i].AutomatedShippingSettings.HasAutomatedShippingSettings'
EXEC sp_OAMethod @json, 'StringOf', @EasyShipShipmentStatus OUT, 'payload.Orders[i].EasyShipShipmentStatus'
EXEC sp_OAMethod @json, 'StringOf', @ElectronicInvoiceStatus OUT, 'payload.Orders[i].ElectronicInvoiceStatus'
SELECT @j = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'payload.Orders[i].PaymentMethodDetails'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @json, 'J', @j
EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'payload.Orders[i].PaymentMethodDetails[j]'
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
PRINT 'Success!'
EXEC @hr = sp_OADestroy @authAws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @sbResponse
EXEC @hr = sp_OADestroy @json
END
GO