Generate a C# SDK
Generate a C# SDK with LWA token generation and authentication.
This tutorial describes how to generate a C# Software Developer Kit (SDK) with Login with Amazon (LWA) token generation and authentication included. It uses the Sellers API
as an example API and Microsoft Windows as the platform, but the steps can be modified for use with other APIs and platforms. Refer to SP-API models for Swagger models for each Selling Partner API section.
You can use this C# SDK to make requests to the Selling Partner API with LWA token generation (generate an access token using your refresh token) and authentication code already set up for you.
Step 1. Set up your workspace
-
On your local drive, create a directory named
SwaggerToCL
. -
Download the software:
-
Run this 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 theC:\SwaggerToCL
directory. -
From the command line, navigate to your local
C:\SwaggerToCL
directory and use this command to clone theselling-partner-api-models
repository:git clone https://github.com/amzn/selling-partner-api-models
-
Navigate to the
selling-partner-api-models\models\sellers-api-model\sellers-api-model
folder in your local copy of the repository. -
Copy the
sellers.json
file and paste it intoC:\SwaggerToCL
. -
Navigate to
selling-partner-api-models/clients/sellingpartner-api-aa-csharp
on GitHub and download thesellingpartner-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.
Step 2. Generate a C# SDK with LWA token generation and authentication
- Open Visual Studio.
- In the
sellingpartner-api-aa-csharp
folder, select theSellingPartnerAPIAuthAndAuthCSharp.sln
file and choose thebuild
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 named csharpConfig.json
. Open an editor and add this code. For packageName
, use the same name as the API:
{"packageName":"SellingPartnerAPI.SellerAPI","targetFramework":"v4.7.2"}
Run this 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
This command uses 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
. Now that you have generated your SDK, you can use it to make calls to the Selling Partner API. For additional instructions refer to Connect to the Selling Partner API using a generated C# SDK.
Updated 1 day ago