# Set up the .NET SDK The following instructions help you set up your environment to develop applications with commercetools using the .NET SDK. This exercise walks you through a hands-on setup. For the full SDK reference, including .NET integration and multi-API support, see [Get started with the .NET SDK](/learning-developer-essentials/dev-tooling/dotnet-sdk-getting-started.md). To follow along, you'll need: - Your preferred .NET IDE or text editor - The .NET runtime Pay special attention to version requirements. ### Install an IDE In this tutorial, we use [Visual Studio Code](https://code.visualstudio.com/) (VS Code) with the [C# Dev Kit](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) extension. If you prefer a full-featured IDE, [Visual Studio Community](https://visualstudio.microsoft.com/vs/community/) is a free alternative. If you don't have VS Code installed, follow the instructions [here](https://code.visualstudio.com/Download). ### Check .NET runtime version The .NET SDK requires .NET Standard 2.1 or later. We recommend .NET 8 or later for new projects. To check the version of .NET installed on your machine, run the following command in your terminal: ```bash title="Check .NET version" dotnet --version ``` If .NET is not installed or the version is too old, download the latest release from [dotnet.microsoft.com](https://dotnet.microsoft.com/download). ## Install the SDK Let's now set up a new project and install the commercetools .NET SDK. ### Step 1: Create a new project Create a new console application in your terminal. We named ours `commercetools-environment`. ```bash title="Create a new console application" dotnet new console -n commercetools-environment cd commercetools-environment ``` Open the project folder in VS Code: ```bash title="Open the project in VS Code" code . ``` Once the project is open, you should see a `Program.cs` file and a `.csproj` file in the Explorer panel. ### Step 2: Install the SDK packages Add the core commercetools SDK package and the Import API package. This learning path uses both: ```bash title="Install SDK packages" dotnet add package commercetools.Sdk.Api dotnet add package commercetools.Sdk.ImportApi dotnet add package Microsoft.Extensions.Logging.Console ``` The `commercetools.Sdk.Api` package provides access to the [HTTP API](/api). The `commercetools.Sdk.ImportApi` package is required for the [Import API module](/learning-developer-essentials/import-api/overview.md) later in this learning path. ### Step 3: Finish project setup Verify the packages installed successfully by running: ```bash title="Restore packages" dotnet restore ``` Open your `.csproj` file and confirm that the three packages appear as `PackageReference` entries, similar to the following: ```xml title="commercetools-environment.csproj" ``` Do you see all the packages listed? Great, you are ready to create a commercetools application! ## Test your setup Let's now check if the SDK is set up correctly. Make sure that you have created an API Client as mentioned [previously](/learning-developer-essentials/prepare-your-work-environment/sdk-setup-process.md#create-an-api-client-in-the-merchant-center) before continuing. ### Step 4: Set up an API Client in your SDK Create a new folder called `impl` inside your project directory. Inside that folder, create a file called `ClientService.cs` and copy the following code into it. ```cs using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using commercetools.Sdk.Api; using commercetools.Sdk.Api.Client; public static class ClientService { public static ProjectApiRoot CreateApiClient() { var services = new ServiceCollection(); var configuration = new ConfigurationBuilder() .AddInMemoryCollection(new List> { new("Client:ApiBaseAddress", "https://api.{region}.commercetools.com/"), new("Client:AuthorizationBaseAddress", "https://auth.{region}.commercetools.com/"), new("Client:ClientId", "{clientID}"), new("Client:ClientSecret", "{clientSecret}"), new("Client:ProjectKey", "{projectKey}") }) .Build(); services.UseCommercetoolsApi(configuration, "Client"); services.AddLogging(); return services.BuildServiceProvider() .GetRequiredService(); } } ``` Then update the following placeholder values to match your API Client: - `{clientID}` - `{clientSecret}` - `{projectKey}` - `{region}` (for both `ApiBaseAddress` and `AuthorizationBaseAddress`) For the region values, see [Hosts](/api/general-concepts.md#hosts) to find the correct region for your Project. ### Step 5: Fetch the Customer data Now let's use the API Client to make the first call to the commercetools API. Create a file called `CustomerFetch.cs` in the root of your project and copy the following code. Remember to update the Customer ID. ```cs using commercetools.Sdk.Api.Client; using Microsoft.Extensions.Logging; public class CustomerFetch { public static async Task Main(string[] args) { ProjectApiRoot projectApiRoot = ClientService.CreateApiClient(); ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddConsole()); ILogger logger = loggerFactory.CreateLogger(); string customerId = "{customerID}"; var customer = await projectApiRoot .Customers() .WithId(customerId) .Get() .ExecuteAsync(); logger.LogInformation("Customer last name: " + customer.LastName); } } ``` ### Step 6: Execute the code Run the application with the following command in your terminal: ```bash title="Run the application" dotnet run ``` If the Customer is fetched successfully, you should see the Customer's last name in the console output. If you receive a `401 Unauthorized` error, double-check that your `clientID`, `clientSecret`, and `projectKey` are correct in `ClientService.cs`. ## Related pages - [Area overview page with navigation](/learning-developer-essentials.md) - [Previous page: Set up the Java SDK](/learning-developer-essentials/prepare-your-work-environment/set-up-the-java-sdk.md) - [Next page: Learning check](/learning-developer-essentials/prepare-your-work-environment/learning-check.md) - [Search documentation and API specs](/search.md)