Connect to the Selling Partner API Using a Generated C# SDK
Connect to the SP-API using a generated C# SDK.
This tutorial describes how to use a generated C# Software Developer Kit (SDK) to make API calls. The SDK exposes classes for configuring your Login with Amazon (LWA) credentials and uses them to generate LWA tokens and sign requests for you. For more information, refer to Generate a C# SDK with LWA token generation and authentication.
Before your application can connect to the Selling Partner API, you must register it and a selling partner must authorize it. Refer to Registering your application and Authorizing Selling Partner API applications.
You must also install the following dependencies via NuGet in Visual Studio:
JsonSubTypes 1.2.0
or newerNewtonsoft.json 12.0.3
or newerRestSharp 106.12.0
RateLimiter 2.2.0
or newer (this will also installComposableAsync.Core
)
Step 1. Configure your LWA credentials
Create an instance of LWAAuthorizationCredentials
using these parameters:
Name | Description | Required |
---|---|---|
ClientId | Your LWA client identifier. For more information, refer to Viewing your application information and credentials. | Yes |
ClientSecret | Your LWA client secret. For more information, refer to Viewing your application information and credentials. | Yes |
RefreshToken | The LWA refresh token. Get this value when the selling partner authorizes your application. For more information, refer to Authorizing Selling Partner API applications. |
No. Include RefreshToken if the operation that you call in the following step requires selling partner authorization. All operations that are not grantless operations require selling partner authorization. If you include RefreshToken , do not include Scopes . |
Scopes | The scope of the LWA authorization grant. Takes the value | No. Include Scopes if the operation that you call in the following step is a grantless operation. If you include Scopes , do not include the RefreshToken . |
Endpoint | The LWA authentication server URI. | Yes |
Example of an operation call that requires selling partner authorization:
using Amazon.SellingPartnerAPIAA;
LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials
{
ClientId = "myClientId",
ClientSecret = "myClientSecret",
RefreshToken = "Aztr|...",
Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"")
};
Example of A grantless operation call
using Amazon.SellingPartnerAPIAA;
LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials
{
ClientId = "myClientId",
ClientSecret = "myClientSecret",
Scopes = new List<string>() { ScopeConstants.ScopeNotificationsAPI, ScopeConstants.ScopeMigrationAPI }
Endpoint = new Uri(""https://api.amazon.com/auth/o2/token"")
};
Step 2. Create an instance of the Sellers API and call an operation
After you configure your LWAAuthorizationCredentials
, you can create an instance of SellersApi
and call an operation.
Example:
SellersApi sellersApi = new SellersApi.Builder()
.SetLWAAuthorizationCredentials(lwaAuthorizationCredentials)
.Build();
Troubleshoot a C# SDK
Restsharp errors: “IRestRequest
is defined in an assembly that is not referenced. You must add a reference to assembly RestSharp Version=105.0.0.0. Culture=neutral. PublicKevToken=598062e77f915f75
”
Select to expand the answer.
Update the Restsharp package to version 106.12.0.0. Check the reference to make sure that version 106.12 updated successfully. If it continues to show 105.0.0.0, remove the old version from the reference and add the 106.12.0.0 package manually from the packages folder with the generated client library."The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)"
Select to expand the answer.
In some scenarios, even though you have the required version in the NuGet
packages for Newtonsoft, it requires manual addition from the package folder in the file system. To resolve this, use the following steps:
-
In Visual Studio, right-click your generated SDK package and choose
Add
, thenReferences
. -
On the references window in the .Net Assembly, choose
browse
. -
Navigate to the generated client library folder in local → packages and add the specific package for Newtonsoft.
“Message : Could not load type of field SellingPartnerAPI.servicesApi.Client.ApiClient:rateLimiter
(3) due to : Could not load file or assembly ComposableAsync.Core, Version=1.1.1.0, Culture=neutral, PublicKeyToken=null
or one of its dependencies."
Select to expand the answer.
If you receive this error after you build and run your code, then the ComposableAsync.Core
dependency package was downloaded but not included as a reference. Make sure to add the dll as a reference manually.
Mapping issues with models
Select to expand the answer.
The ItemAsin
model was generated with the JsonObject
type, but the required string ItemAttributes
requires the JsonObjectAttribute
. You can use this command, which uses the Catalog Items API, to import the required mapping for the models :
`java -jar swagger-codegen-cli.jar generate -i catalogItems_2022-04-01.json -l csharp -t sellingpartner-api-aa-csharp\src\Amazon.SellingPartnerAPIAA\resources\swagger-codegen\templates —import-mappings ItemAttributes=Newtonsoft.Json.JsonObjectAttribute —import-mappings ItemAsin=System.string -o catalogItems2022-04-01_import`
"HttpRequestHeaders.ToJson()
: no suitable method found to override."
Select to expand the answer.
Modify the override
keyword with virtual
. Declare methods as virtual
so that they can be overridden.
public virtual string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
“The name BaseValidate
does not exist in the current context”
Select to expand the answer.
Uncomment the mentioned methods in the class:
// IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
// {
// foreach (var x in BaseValidate(validationContext))
// yield return x;
// yield break;
// }
Updated about 2 months ago