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

# Update a Delivery API key

PUT https://api.contentful.com/spaces/{space_id}/api_keys/{api_key_id}
Content-Type: application/vnd.contentful.management.v1+json

Updates a Delivery API key and its corresponding Preview API key. Include the current version in `X-Contentful-Version`.

Reference: https://contentful.com/developers/docs/references/content-management-api/api-keys/update-a-delivery-api-key

## Authentication

- `Authorization` header (bearer token, required) — Bearer authentication of the form `Bearer <token>`, where token is your auth token.

## Request

### Path parameters

- `space_id` (string, required) — ID of the space in a form of a string
- `api_key_id` (string, required) — ID of the Delivery API key in a form of a string

### Headers

- `X-Contentful-Version` (string, required) — Version of the API key to update

### Body (application/vnd.contentful.management.v1+json)

This endpoint expects a map from string to any.

- `map from string to any`

## Response

### 200

OK - Request successful

- `map from string to any`

## Examples

**Request**

```json
{
  "environments": [
    {
      "sys": {
        "id": "staging",
        "linkType": "Environment",
        "type": "Link"
      }
    }
  ],
  "name": "Example API Key"
}
```

**Response**

```json
{
  "accessToken": "b4c0n73n7fu1",
  "description": null,
  "environments": [
    {
      "sys": {
        "id": "staging",
        "linkType": "Environment",
        "type": "Link"
      }
    }
  ],
  "name": "Example API Key",
  "preview_api_key": {
    "sys": {
      "id": "1Mx3FqXX5XCJDtNpVW4BZI",
      "linkType": "PreviewApiKey",
      "type": "Link"
    }
  },
  "sys": {
    "createdAt": "2025-02-08T22:43:15.000Z",
    "createdBy": {
      "sys": {
        "id": "7BslKh9TdKGOK41VmLDjFZ",
        "linkType": "User",
        "type": "Link"
      }
    },
    "id": "exampleapikey",
    "space": {
      "sys": {
        "id": "yadj1kx9rmg0",
        "linkType": "Space",
        "type": "Link"
      }
    },
    "type": "ApiKey",
    "updatedAt": "2025-02-08T22:43:15.000Z",
    "updatedBy": {
      "sys": {
        "id": "4FLrUHftHW3v2BLi9fzfjU",
        "linkType": "User",
        "type": "Link"
      }
    },
    "version": 1
  }
}
```

**SDK Code**

```android Android
// Create the Contentful client.
final CMAClient client =
    new CMAClient
        .Builder()
        .setAccessToken("<access_token>")
        .setSpaceId("<space_id>")
        .setEnvironmentId("<environment_id>")
        .build();

// Update a new Key.
final CMAApiKey key = new CMAApiKey()
  .setName("NewApiKey_" + Math.random())
  .setDescription("SomethingDescriptive");

// Upload it to Contentful.
client
  .apiKeys()
  .async()
  .update(
    key,
    new CMACallback<CMAApiKey>() {
      @Override protected void onSuccess(CMAApiKey result) {
        new AlertDialog.Builder(context)
        .setTitle("Contentful")
        .setMessage("Successfully updated a new api keys." +
        "\n\nResult: " + result)
        .show();
}

          @Override protected void onFailure(RuntimeException exception) {
            // An error occurred! Inform the user.
            new AlertDialog.Builder(context)
                .setTitle("Contentful Error")
                .setMessage("Could not update api keys." +
                    "\n\nReason: " + exception.toString())
                .show();

            super.onFailure(exception);
          }
        }
    );
```

```java Java
// Create the Contentful client.
final CMAClient client =
    new CMAClient
        .Builder()
        .setAccessToken("<access_token>")
        .setSpaceId("<space_id>")
        .setEnvironmentId("<environment_id>")
        .build();

// Update a single delivery key.

final CMAApiKey key = new CMAApiKey()
  .setId("keyid")
  .setSpaceId("spaceid")
  .setVersion(4)
  .setName("Master Key - Do not update")
  .addEnvironment("staging");

final CMAApiKey deliveryApiKey =
  client
  .apiKeys()
  .update(key);
```

```kotlin Kotlin
// Create the Contentful client.
val client = CMAClient.Builder()
    .setAccessToken("<access_token>")
    .setSpaceId("<space_id>")
    .setEnvironmentId("<environment_id>")
    .build()

// update a single delivery key

val key = CMAApiKey().apply {
  setId<CMAApiKey>("keyid")
  setSpaceId<CMAApiKey>("spaceid")
  setVersion<CMAApiKey>(4)
  name = "Master Key - Do not update"
  addEnvironment("staging")
}

val apiKeys = client
        .apiKeys()
        .update(key)
```

```swift Swift
We currently don't provide a CMA SDK for this platform.
```

```js-legacy JavaScript (legacy)
import { createClient } from 'contentful-management'

const client = createClient({
  accessToken: '<content_management_api_key>'
}, { type: 'legacy' })

client.getSpace('<space_id>')
.then((space) => space.getApiKey('<delivery_api_key_id>'))
.then((apiKey) => {
 apiKey.name = 'New name';
 apiKey.environments = [
    {
        sys: {
            type: 'Link',
            linkType: 'Environment',
            id:'<environment_id>'
        }
    }
 ];
 return apiKey.update();
})
.then(apiKey => console.log(apiKey.name))
.catch(console.error);
```

```javascript JavaScript
import { createClient } from 'contentful-management'

// Client init
const client = createClient({
  accessToken: '<content_management_api_key>'
})

let key = await client.apiKey.get({
  spaceId: '<space_id>',
  apiKeyId: '<delivery_api_key_id>',
});

key = await client.apiKey.update(
  {
    spaceId: '<space_id>',
    apiKeyId: '<delivery_api_key_id>',
  },
  {
    ...key,
    name: 'New Name',
  }
);
```

```php PHP
$client = new \Contentful\Management\Client('<content_management_api_key>');

$space = $client->getSpaceProxy('<space_id>');

$deliveryApiKey = $space->getDeliveryApiKey('<delivery_api_key_id>');

$deliveryApiKey->setName('Website');
$deliveryApiKey->update();
```

```ruby Ruby
require 'contentful/management'

client = Contentful::Management::Client.new('<content_management_api_key>')

api_key = client.api_keys('<space_id>').find('<delivery_api_key_id>')

new_environment = {
  sys: {
    id: 'testing',
    type: 'Link',
    linkType: 'Environment'
  }
}

api_key.update(
  name: 'New API Key Name',
  environments: api_key.environments.map(&:raw_object) << new_environment
)
```

```python Python
from contentful_management import Client

client = Client('<content_management_api_key>')

api_key = client.api_keys('<space_id>').find('<delivery_api_key_id>')

api_key.name = 'New API Key Name'

new_environment_link = Link({
    "sys": {
        "type": "Link",
        "linkType": "Environment",
        "id": "staging"
    }
})
api_key.environments.append(new_environment_link)

api_key.save()
```

```.net .NET
// This client should only be created once per application.
var httpClient = new HttpClient();

var client = new ContentfulManagementClient(httpClient, "<content_management_api_key>", "<space_id>");

var updatedKey = await client.UpdateApiKey("<delivery_api_key_id>", "Example API Key", "Description of key", version: 1);
```