# How to Fetch Available Environments from Dynamics 365 CRM

While working with Microsoft Dynamics 365 and the Power Platform, having visibility into all available environments is crucial. This information comes in handy for automating processes, managing integrations, and handling administrative tasks. In this guide, we’ll explore how to retrieve environment details using the Web API.

## Prerequisites

Before you start, make sure you have:

* An app registered in **Azure Active Directory**
    
* The proper **API permissions** for Dynamics CRM
    
* Your **Client ID** and **Client Secret**
    

## Step 1: Get an Access Token

You first need to authenticate with Azure AD. Send a `POST` request to the token endpoint:

```apache
POST: https://login.microsoftonline.com/{tenant_id}/oauth2/token
```

**Headers:**

```javascript
Content-Type: application/x-www-form-urlencoded
```

**Body:**

```apache
grant_type=client_credentials
client_id={your_client_id}
client_secret={your_client_secret}
resource=https://yourorg.crm.dynamics.com/
```

This will return an **access token** you can use to call the API.

## Step 2: Fetch Environments

With the token, send a `GET` request to the Power Platform environments API:

```apache
GET https://api.powerplatform.com/providers/Microsoft.BusinessAppPlatform/environments?api-version=2020-10-01
```

**Headers:**

```javascript
Authorization: Bearer {access_token}
Content-Type: application/json
```

## Step 3: Review the Response

The response will include all available environments with details like:

```javascript
{
  "value": [
    {
      "id": "12345",
      "name": "Default",
      "location": "North America",
      "state": "Ready"
    },
    {
      "id": "67890",
      "name": "Sandbox",
      "location": "Europe",
      "state": "Ready"
    }
  ]
}
```

## Step 4: Use the Data

Now that you have the list, you can use it in your application—for example, to let users pick which environment they want to work with.

## Conclusion

Fetching available environments is a simple but powerful step when working with Dynamics 365 CRM. It allows you to dynamically manage solutions, automate processes, and build more flexible applications.
