> This page is for Contentful SDKs.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://contentful-staging.ferndocs.com/developers/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://contentful-staging.ferndocs.com/developers/docs/_mcp/server.

# Working with Contentful environments and .NET

> This article details how to work with environments using the .NET Content Deliver API client library | Configuring your environment | Fetching content

To make development easier for our users, we publish client libraries for various languages which make the task easier. This article details how to use the [.NET CDA client library](https://github.com/contentful/contentful.net) with environments.

## Pre-requisites

This tutorial assumes you understand the basic Contentful data model as described in the [developer center](/concepts/data-model) and that you have
already read and understand the [getting started tutorial for the .NET client library](/sdks/net/tutorials/getting-started-with-contentful-and-net) and the [using the management API with Contentful and .NET client library](/sdks/net/tutorials/using-the-management-api-with-contentful-and-net) article.

Contentful.net is built on .NET Core and targets .NET Standard 2.0. The client library is cross-platform and runs on Linux, macOS, and Windows.

## Configuring your environment

The `ContentfulOptions` class has an `Environment` property that you can set to configure which environment you want to fetch content from. If omitted it will default to the master environment.

```csharp
var httpClient = new HttpClient();
var options = new ContentfulOptions {
    DeliveryApiKey = "<content_delivery_api_key>",
    SpaceId = "<space_id>",
    Environment = "<environment_id>"
};
var client = new ContentfulClient(httpClient, options);
```

If you're using ASP.NET Core you can set the environment in your `appsettings.json`.

```json
"ContentfulOptions": {
    "DeliveryApiKey": "<access_token>",
    "ManagementApiKey": "<cma_access_token>",
    "PreviewApiKey": "<preview_access_token>",
    "SpaceId": "<space_id>",
    "UsePreviewApi": false,
    "MaxNumberOfRateLimitRetries": 0,
    "Environment": "<environment_id>"
  }
```

## Fetching content from an environment

Once an environment is configured, subsequent requests to all environment aware endpoints will use that environment. For example calling `GetEntries` with an environment configured will result in fetching entries from that environment.

```csharp
var httpClient = new HttpClient();
var options = new ContentfulOptions {
    DeliveryApiKey = "<content_delivery_api_key>",
    SpaceId = "<space_id>",
    Environment = "staging"
};
var client = new ContentfulClient(httpClient, options);

var entries = await client.GetEntries<Product>();
// entries would be a collection of entries from the "staging" environment
```

The same is true for calling `GetAssets`, `GetContentTypes`. In fact *all* the methods of the CDA (Content Delivery API) client, except the sync methods, are environment aware.

However, when using the `ContentfulManagementClient`, only a subset of the methods available on the `ContentfulManagementClient` are environment aware. The other methods will call your master environment, *regardless of wether you have an environment configured or not*. The following methods are **not** environment aware:

* `CreateSpace`
* `UpdateSpaceName`
* `GetSpace`
* `GetSpaces`
* `DeleteSpace`
* `GetWebhooksCollection`
* `CreateWebhook`
* `CreateOrUpdateWebhook`
* `GetWebhook`
* `DeleteWebhook`
* `GetWebhookCallDetailsCollection`
* `GetWebhookCallDetails`
* `GetWebhookHealth`
* `GetRole`
* `GetAllRoles`
* `CreateRole`
* `UpdateRole`
* `DeleteRole`
* `GetSpaceMemberships`
* `CreateSpaceMembership`
* `GetSpaceMembership`
* `UpdateSpaceMembership`
* `DeleteSpaceMembership`
* `GetAllApiKeys`
* `CreateApiKey`
* `GetAllUsers`
* `GetUser`
* `GetCurrentUser`
* `GetUpload`
* `UploadFile`
* `DeleteUpload`
* `CreateManagementToken`
* `GetAllManagementTokens`
* `GetManagementToken`
* `RevokeManagementToken`
* `GetOrganizations`
* `GetEnvironments`
* `CreateEnvironment`
* `CreateOrUpdateEnvironment`
* `GetEnvironment`
* `DeleteEnvironment`

This means that a call to *any* of the above methods will *always* use your master environment.

```csharp
var httpClient = new HttpClient();
var options = new ContentfulOptions {
    ManagementApiKey = "<content_management_api_key>",
    SpaceId = "<space_id>",
    Environment = "staging"
};
var client = new ContentfulManagementClient(httpClient, options);

var contentTypes = await client.GetContentTypes();
// would get all the content types from the "staging" environment

await client.DeleteWebhook("<webhook_id>");
// Will delete the webhook from the master environment!
```

The above example shows how you can fetch content types from a specific environment, but the call to `DeleteWebhook` will actually delete the webhook from the *space* as webhooks are currently not environment aware. It is therefore very important to understand which endpoints affect an environment and which affect the entire space to avoid involuntary data loss.

## Next steps

* [Explore the .NET client library GitHub repository](https://github.com/contentful/contentful.net)