Automate your SP-API calls using C# SDK
Tutorial to automate your SP-API calls with C# SDK with Login with Amazon (LWA) token exchange and authentication.
by Supriya P., Solutions Architect, Selling Partner Developer Services | February 7, 2023
If you are a developer looking to programmatically access your SP-APIs, you can automate the process with a Software Development Kit (SDK). An SDK bundles multiple steps together, such as requesting an access token, requesting a security token, and signing requests with Signature Version 4 (SigV4), to call the SP-APIs seamlessly. This post provides you with all the required details to generate a C# SDK with Login with Amazon (LWA) token exchange and authentication to build your application seamlessly. You learn the prerequisites required to build the C# SDK and also see an example using the Selling Partner API for Sellers and Swagger Code Generator. For more information on integrating with our Java SDK, refer to the Automate your SP-API calls using Java SDK blog.
Tutorial
The following tutorial will help you set up your own C# SDK for automating SP-API calls.
Prerequisites
To complete this tutorial, you need the following prerequisites:
- A hybrid or SP-API app in draft or published state
- IDE software (this walkthrough uses Visual Studio IDE on Windows OS)
- An IAM user associated with your application ID in Seller Central or Vendor Central. For details, see Creating and configuring IAM policies and entities.
If you do not have a hybrid or SP-API app, follow the steps to register as a developer and register your application. Then, return to this blog post.
Next, set up your workspace for the tutorial.
Step 1. Set up your workspace
- On your local drive, create a directory for this project and name it
SwaggerToCL
. - Download the following tools.
- Run the following command to download the Swagger Code Generator:
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.13/swagger-codegen-cli-2.4.13.jar -O swagger-codegen-cli.jar
- Copy
swagger-codegen-cli.jar
into your local directoryC:\\SwaggerToCL
. - In GitHub, go to https://github.com/amzn/selling-partner-api-models/tree/main/models and use the following command to clone the selling-partner-api-models repository to your local directory
C:\\SwaggerToCL
.
git clone https://github.com/amzn/selling-partner-api-models
- Navigate to the
selling-partner-api-models\\models
folder in your local copy of the repository and copy a JSON file from the models subfolders intoC:\\SwaggerToCL
. This blog post uses theSellers.json
file. - In GitHub, go to https://github.com/amzn/selling-partner-api-models/tree/main/clients/sellingpartner-api-aa-csharp and download the sellingpartner-api-aa-csharp folder to your local computer.
This folder provides helper classes to generate an access token and sign the requests for Amazon Selling Partner APIs. It is intended for use with the Selling Partner API Client Libraries generated by Swagger Codegen using the RestSharp library. It can also be integrated into custom projects.
Now that you have completed the required setup, the next step is to generate the C# SDK with the authentication and authorization classes provided in the sellingpartner-api-aa-csharp folder.
Step 2. Generate a C# SDK with LWA token exchange and authentication
To learn more about authorization automation works, refer to How does authorization automation work.
- Open Visual Studio.
- In the sellingpartner-api-aa-csharp folder, select the
SellingPartnerAPIAuthAndAuthCSharp.sln
file and build it using the build option in Visual Studio. This generates theAmazon.SellingPartnerAPIAA.dll
assembly in the foldersellingpartner-api-aa-csharp\\src\\Amazon.SellingPartnerAPIAA\\bin\\Debug\\netstandard2.0
. - Open a terminal and run the following commands to generate the C# client library. The default package name for the generated client library is
Swagger.IO
. These commands generate the client libraries with their respective API names as the package name instead ofSwagger.IO
.- In
C:\\SwaggerToCL
, create a JSON file namedcsharpConfig.json
, open an editor and add the following code. ForpackageName
, use the same name of the API you want to generate the client library for:
- In
{"packageName":"SellingPartnerAPI.SellerAPI","targetFramework":"v4.7.2"}
Run the following command to generate C# code with a customized package name:
java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\[name of model].json -l csharp -t [path to selling-partner-api-models\clients\sellingpartner-api-aa-csharp folder]\src\Amazon.SellingPartnerAPIAA\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\[name of client library] -c C:\SwaggerToCL\csharpConfig.json
For example, here is the command using Sellers.json
to generate C# code:
java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\Sellers.json -l csharp -t C:\SwaggerToCL\sellingpartner-api-aa-csharp\src\Amazon.SellingPartnerAPIAA\resources\swagger-codegen\templates\ -o C:\SwaggerToCL\Sellers_CsharpCL -c C:\SwaggerToCL\csharpConfig.json
The SDK is created in C:\\SwaggerToCL\\Sellers_CsharpCL
. The next step is to write the actual application and connect to the SP-API using the generated C# SDK.
Step 3. Connect to the Selling Partner API using the generated C# SDK
These steps use Visual Studio IDE to connect to the Selling Partner API:
- In Visual Studio IDE, navigate to your generated library Sellers_CsharpCL folder and open the
.sln
file. The.sln
package file will have the name as provided in Step 2. In this example, the package file name isSellingPartnerAPI.SellerAPI.sln
. - Right-click the
SellingPartnerAPI.SellerAPI
file and select Add > References. - In the References window, choose Browse, navigate to
sellingpartner-api-aa csharp/src/Amazon.SellingPartnerAPIAA/bin/Debug/netstandard2.0
and chooseAmazon.SellingPartnerAPIAA.dll
. - Repeat Step 2 and Step 3 for the
SellingPartnerAPI.SellerAPI.Test
file. - Navigate to your generated library
Sellers_CsharpCL
folder, right-click theSellingPartnerAPI.SellerAPI
file, choose Options > General, and change the target framework to .net Framework v4.7 and above. - Repeat Step 5 for the
SellingPartnerAPI.SellerAPI.Test
file. - Right-click the
SellingPartnerAPI.SellerAPI
file and choose Manage NuGet Packages. Make sure you have the following package versions (If not, install the respective NuGet packages):- JsonSubTypes 1.2.0 or newer
- Newtonsoft.json 12.0.3 or newer
- RestSharp 106.15.0
- RateLimiter 2.2.0 or newer (this will also install ComposableAsync.Core)
- Right-click the
SellingPartnerAPI.SellerAPI.Test
file and choose Manage NuGet Packages. Install the Nunit 2.6.4 package.
Manual installation of NuGet packages
In some scenarios (for example, using IOS), the required versions in the NuGet packages appear, but you may encounter an error. In this scenario, you must manually add the packages from the package folder in the file system:
- In Visual Studio IDE, navigate to your generated library
Sellers_CsharpCL
folder and open the.sln
file.- Right-click the
SellingPartnerAPI.SellerAPI
file and select Add > References.- In the References window, choose Browse, navigate to
sellingpartner-api-aa csharp/src/Amazon.SellingPartnerAPIAA/bin/Debug/netstandard2.0
, choose the generated client library folder (for example,Sellers_CsharpCL
, then choose packages and add the package for which you are facing the error.
- In the
SellingPartnerAPI.SellerAPI.Test > Api
folder, open the[Modelname]Tests.cs
file and add the following code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using RestSharp;
using SellingPartnerAPI.SellerAPI.Client;
using SellingPartnerAPI.SellerAPI.Api;
using SellingPartnerAPI.SellerAPI.Model;
using Amazon.SellingPartnerAPIAA;
namespace SellingPartnerAPI.SellerAPI.Test
{
class SellersApiTests
{
public static void Main(string[] args)
{
LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials
{
ClientId = "amzn1.application-oa2-client.******************",
ClientSecret = "***********************************",
RefreshToken = "Atzr|***********************************",
Endpoint = new Uri("https://api.amazon.com/auth/o2/token")
};
AWSAuthenticationCredentials awsAuthenticationCredentials = new AWSAuthenticationCredentials
{
AccessKeyId = "*********************",
SecretKey = "***********************",
Region = "us-east-1"
};
SellersApi sellersApi = new SellersApi.Builder()
.SetAWSAuthenticationCredentials(awsAuthenticationCredentials)
.SetLWAAuthorizationCredentials(lwaAuthorizationCredentials)
.Build();
GetMarketplaceParticipationsResponse result = sellersApi.GetMarketplaceParticipations();
Console.WriteLine(result.ToJson());
}
}
}
- To view the output of the code, you need to convert it to a console application. To do so, right-click the
SellingPartnerAPI.SellerAPI.Test
file and choose Options. - On the Project Options window, in the left navigation pane, expand Build, then choose General. In the Code Generation section, choose Compile Target and select Executable with GUI. Choose OK.
- Build and run the project.
Conclusion
In this blog, you learned how to automate your SP-API calls using a C# SDK. In the tutorial, you learned how to set up your workspace, generate a C# SDK for the Selling Partner API, connect to the Sellers API, and make an API call.
Have feedback on this post?
If you have questions or feedback on this post, we'd like to hear from you! Please vote and leave a comment using the tools at the bottom of this page.
Subscribe to updates via RSS feed.
Updated about 1 month ago