commercetools.Sdk
The commercetools platform, import-api and C# sdks generated from our api reference.
|
This repository contains the .NET SDKs generated from the Composable Commerce API reference.
Package | Version |
---|---|
HTTP API | |
Import API | |
Change History API |
Feel free to explore these examples using this SDK for calling the Composable Commerce HTTP API, and Import API. .NET Core SDK Training for V2 SDK
Package | Installation |
---|---|
HTTP API | dotnet add package commercetools.Sdk.Api |
Import API | dotnet add package commercetools.Sdk.ImportApi |
Change History API | dotnet add package commercetools.Sdk.HistoryApi |
The SDK consists of the following projects:
commercetools.Base.Abstractions
: Contains common classes and interfaces that can be used in other SDK projects like IClient and ISerializerService.commercetools.Base.Client
: Contains CtpClient which communicate with Composable Commerce to execute requests, it contains also the classes related to the client like tokens,middlewares and handlers.commercetools.Base.Registration
: Helper classes for things like types retriever.commercetools.Base.Serialization
: Serialization and deserialization services for responses and requests to the HTTP API using System.Text.Json.commercetools.Sdk.Api
: Contains all generated models and request builders to communicate with Composable Commerce HTTP API.commercetools.Sdk.ImportApi
: Contains all generated models and request builders to communicate with the Import API.commercetools.Sdk.HistoryApi
: Contains all generated models and request builders to communicate with the Change History API.commercetools.Sdk.GraphQL.Api
: Contains a type safe GraphQL client to communicate with Composable Commerce HTTP API.In addition, the SDK has the following directories:
/IntegrationTests
: Integration tests for the SDK. A good way for anyone using the .NET SDK to understand it further./Tests
: Unit Tests for serialization and request builders./Examples
:commercetools.Api.ConsoleApp
as an example of how to use the SDK in a console app and create a client using ClientFactory.commercetools.Api.CheckoutApp
, the example is developed to illustrate how to deal with the me endpoints, like listing and adding products to cart as anonymous user, user can login and switch to password token flow, it's storing the token in the cookie right now, cookies only used as POC, it maybe not the best option for production purposes.All operations (get, query, create, update, delete and others) are available in the generated request builders, so you can build the request and use the client to execute it and all the request builders accessible through ApiRoot. In order to use the client object, it needs to be setup first through dependency injection setup.
At a high level, to make a basic call to the API, do the following:
In the ConfigureServices method of Startup.cs add the following:
Composable Commerce HTTP API
: Import API
: Change History API
: The client configuration needs to be added to appsettings.json in order for the client to work. The structure is as follows:
you can use the instance inside the injected client or use ApiFactory to create a new instance.
Composable Commerce HTTP API
: Import API
: Change History API
: The ProjectApiRoot is scoped to the projectKey in order to not have to provide the project for building requests. You can use the instance inside the injected client or use ApiFactory to create a new instance.
Composable Commerce HTTP API
: Import API
: Change History API
: When using the UseCommercetools methods a ProjectApiRoot will be registered to the ServiceProvider using the project key given in the configuration
It is possible to use more than one client in the same application with different token providers. The following code can be used to set it up 2 clients with the default ClientCredentials token provider:
you can choose different token provider based on the clientName, Also The appsettings.json then needs to contain the configuration sections named the same. The clients can then be injected by using IEnumerable and specific client can be selected by name.
SDK follows a builder pattern when creating requests. Category resource will be used to demonstrate how to use the SDK. This behaviour is the same for all resources. The IClient interface can be used by injecting it and calling its ExecuteAsync method for different requests.
You can get a client from injected services or you can create client on the fly using ClientFactory, the example below illustrate how to create a client with password TokenFlow to get customer's orders:
If you are going to create the client using the ClientFactory, don't forget to call SetupClient extension method here while building the services container. This will attach the Default Handlers like ErrorHandler and LoggerHandler to the Client like below:
To migrate from the 1.x to the 2.x, there is a guideline below: