Use C# SDKs generated in Postman
Use C# SDKs generated in Postman
Use C# SDKs generated in Postman
This guide provides instructions for using C# SDKs generated by the Postman SDK Generator. It covers how to install the SDK into your project, run example code, and best practices for using the SDK effectively in your applications.
The C# SDK includes the following key files:
[SdkName]Client.cs — The main SDK client that implements IDisposable and provides access to all services.Services/[ServiceName]Service.cs — Service classes with async API methods.Models/[ModelName].cs — Data models (records or classes with System.Text.Json serialization).Config/[SdkName]Config.cs — SDK-level configuration record (environment, auth, timeout).Config/RequestConfig.cs — Per-request configuration overrides.Config/RetryConfig.cs — Retry policy configuration.Config/ApiKeyAuthConfig.cs — API key authentication configuration (generated when applicable).Config/BasicAuthConfig.cs — Basic authentication configuration (generated when applicable).Http/Environment.cs — Environment class with static named environment properties.Http/ApiException.cs — Base exception class for all API errors.Http/Exceptions/[ModelName]Exception.cs — Typed exception classes for spec-defined error models.Models/Optional.cs — An Optional<T> wrapper for optional fields.Example/Program.cs — Runnable example demonstrating SDK usage.[SdkName].csproj — .NET project and NuGet package configuration.The following is an example of the typical structure of a generated C# SDK:
Each generated SDK includes an Example/Program.cs file demonstrating SDK initialization, making an API call, and basic error handling.
The example includes the following features:
Example/Program.cs
You can install the generated C# SDK into your project using NuGet or by referencing it directly from the filesystem.
To install the SDK from NuGet, run:
Then use it in your code:
If the SDK isn’t published to NuGet yet, you can reference it directly from the filesystem using ProjectReference in your .csproj file.
Alternatively, build the SDK as a NuGet package and add it to a local feed:
Publishing your SDK to NuGet.org makes it available to any .NET developer using dotnet add package.
To publish your SDK to NuGet, you need the following:
The generated [SdkName].csproj already includes the metadata required for publishing. Review and update the following fields before publishing.
To publish your SDK to NuGet, do the following:
Build and pack the SDK.
Publish to NuGet.
Verify the publication at https://www.nuget.org/packages/YourSdk. Packages are typically indexed within 15–30 minutes.
To publish updates to your SDK, run the following:
Use the following best practices when publishing your C# SDK to NuGet:
<GenerateDocumentationFile>true</GenerateDocumentationFile> in your .csproj file..snupkg symbols package for debugging support by adding <IncludeSymbols>true</IncludeSymbols> and <SymbolPackageFormat>snupkg</SymbolPackageFormat>.The following sections cover advanced usage topics for the generated C# SDK, including authentication, asynchronous calls, environment management, hierarchical configuration, error handling, and timeouts and retries.
The SDK supports various authentication methods. Configure authentication when creating the configuration record passed to the SDK client constructor.
The SDK includes authentication support based on the security schemes defined in your API specification. The following examples show the possible authentication methods.
Each generated auth property is only present in the configuration when the corresponding auth type is defined in the specification.
All service methods are asynchronous and follow the standard .NET async/await pattern. Every method accepts an optional CancellationToken as its last parameter, allowing you to cancel in-flight requests.
The SDK generates an Environment class with static properties for each named server defined in your API specification.
Available environments are defined in Http/Environment.cs as static properties. If your API specification includes servers with names like “production”, “development”, or “staging”, those are generated as properties on the Environment class for easy reference.
C# supports four levels of configuration. Each level overrides the previous, with request-level being the most specific.
RequestConfig is a C# record with optional properties. Only the values you provide override the inherited configuration; omitted properties fall through to the next level.
Service methods throw exceptions on API errors. The base exception class is ApiException, which wraps the underlying HttpResponseMessage. Catch ApiException for general error handling.
To handle specific status codes, run the following:
When the API specification defines typed error response models (for example, a NotFoundError schema on a 404 response), the SDK generates a corresponding typed exception class that extends ApiException. Access the deserialized error model through the named property on the exception:
To set a global timeout, run the following:
The timeout can also be set at construction time through RequestConfig on the config:
To configure retries, pass a RetryConfig record at construction time or per-request:
The following table shows the default retry configuration values used by the SDK if no RetryConfig is provided:
The SDK validates required parameters before sending the request. If a required argument is null, the service method throws an ArgumentNullException immediately, without making an HTTP call.
No manual validation calls are needed. Validation is built into every service method.
The SDK uses two patterns to represent optional and nullable fields, providing a clear distinction between undefined, null, and present values. This is important when building request models, as omitting an optional field versus explicitly setting it to null produces different JSON payloads.
null.string?, long?) and must be provided, but may be null.Optional<T> struct. This wrapper distinguishes between “not provided” (field omitted from JSON) and “provided with a value” (field included in JSON, even if null for Optional<T?>).The following examples show the full range of field combinations:
When reading responses, Optional<T> fields deserialized from the server response will have IsProvided = true if the field was present in the JSON (even if null for nullable types), and IsProvided = false if the field was absent.
The SDK client implements IDisposable and manages the lifecycle of its internal HttpClient. Always dispose of the client when you are finished using it to release HTTP resources.
Consider the following resources for using and customizing your C# SDK:
documentation/ directory inside your SDK for per-service and per-model reference pages.<GenerateDocumentationFile>true</GenerateDocumentationFile> in the .csproj file and browse IntelliSense docs directly in your IDE.https://www.nuget.org/packages/YourSdk.Example/Program.cs for a runnable demonstration of the SDK.System.Text.Json for serialization and Polly for retry resilience.