# API Keys

When performing operations on `API Keys` for a specific user, you must be authenticated using a Production API Key.

---

## API Keys object

| Property | Type | Description |
|----------|------|-------------|
| id | string | The ID of the authenticated User making the request |
| children | API Keys array | A list of all Child Users presented with ONLY id, children, and keys |
| keys | API Keys array | The list of all API keys active for an account, both for "test" and "production" modes. |

Example Object:

```json
{
  "id": "user_060ab38db3c04ffaa60f262e5781a9be",
  "keys": [
    {
      "object": "ApiKey",
      "active": true,
      "key": "<REDACTED>",
      "mode": "test",
      "created_at": "2022-10-14T17:23:58Z",
      "id": "ak_eb495dfd453647749dc6471290b33685"
    },
    {
      "object": "ApiKey",
      "active": true,
      "key": "<REDACTED>",
      "mode": "production",
      "created_at": "2022-10-14T17:23:58Z",
      "id": "ak_6f4eae3909464f3f807b3af2a687c85b"
    }
  ],
  "children": [
    {
      "id": "user_0ae8cb7000a1438c8598fa5786fdae84",
      "keys": [
        {
          "object": "ApiKey",
          "active": true,
          "key": "<REDACTED>",
          "mode": "test",
          "created_at": "2022-10-17T17:28:30Z",
          "id": "ak_3b56830ab851496f946c368cb001c3f4"
        },
        {
          "object": "ApiKey",
          "active": true,
          "key": "<REDACTED>",
          "mode": "production",
          "created_at": "2022-10-17T17:28:30Z",
          "id": "ak_e5020ebb06b84733891c0e0e5ce25b27"
        }
      ],
      "children": []
    }
  ]
}
```

---

## API Key object

| Property | Type | Description |
|----------|------|-------------|
| id | string | The ID of the API Key |
| object | string | "ApiKey" |
| mode | string | "test" or "production" |
| key | string | The actual key value to use for authentication |
| created_at | datetime | When the API Key was created |
| active | boolean | Whether or not the API Key is active |

Example Object:

```json
{
  "id": "user_060ab38db3c04ffaa60f262e5781a9be",
  "keys": [
    {
      "object": "ApiKey",
      "active": true,
      "key": "<REDACTED>",
      "mode": "test",
      "created_at": "2022-10-14T17:23:58Z",
      "id": "ak_eb495dfd453647749dc6471290b33685"
    },
    {
      "object": "ApiKey",
      "active": true,
      "key": "<REDACTED>",
      "mode": "production",
      "created_at": "2022-10-14T17:23:58Z",
      "id": "ak_6f4eae3909464f3f807b3af2a687c85b"
    }
  ],
  "children": [
    {
      "id": "user_0ae8cb7000a1438c8598fa5786fdae84",
      "keys": [
        {
          "object": "ApiKey",
          "active": true,
          "key": "<REDACTED>",
          "mode": "test",
          "created_at": "2022-10-17T17:28:30Z",
          "id": "ak_3b56830ab851496f946c368cb001c3f4"
        },
        {
          "object": "ApiKey",
          "active": true,
          "key": "<REDACTED>",
          "mode": "production",
          "created_at": "2022-10-17T17:28:30Z",
          "id": "ak_e5020ebb06b84733891c0e0e5ce25b27"
        }
      ],
      "children": []
    }
  ]
}
```

---

**Production Only**

## Create an API Key

### Example: POST /api_keys

#### cURL

```shell
curl -X POST https://api.easypost.com/v2/api_keys \
  -u "EASYPOST_API_KEY": \
  -H 'Content-Type: application/json' \
  -d '{
    "mode": "test"
  }'
```

  **Important**: This endpoint only works for `ReferralCustomers` or to
  manage Child `User`accounts. If you feel this is applicable for your use case,
  please contact sales.

Both Production and Test API Keys can be created for a `User` and all of its children.
If the request is authenticated as a Child `User`, only the API Keys for that Child `User` will be created.

---

**Production Only**

## Disable an API Key

### Example: POST /api_keys/:id/disable

#### cURL

```shell
curl -X POST https://api.easypost.com/v2/api_keys/ak_.../disable \
  -u "EASYPOST_API_KEY":
```

  **Important**: This endpoint only works for `ReferralCustomers` or to
  manage Child `User`accounts. If you feel this is applicable for your use case,
  please contact sales.

Disabling an `API Key` immediately renders it unusable.
You can disable Production and Test API Keys, but you must use your Production API Key to authenticate.
Please exercise caution when disabling `API Keys`.

---

**Production Only**

## Enable an API Key

### Example: POST /api_keys/:id/enable

#### cURL

```shell
curl -X POST https://api.easypost.com/v2/api_keys/ak_.../enable \
  -u "EASYPOST_API_KEY":
```

  **Important**: This endpoint only works for `ReferralCustomers` or to
  manage Child `User`accounts. If you feel this is applicable for your use case,
  please contact sales.

You can enable an `API Key` if it is disabled.

---

**Production Only**

## Retrieve an API Key

### Example: GET /api_keys

#### cURL

```shell
curl -X GET https://api.easypost.com/v2/api_keys \
  -u "EASYPOST_API_KEY":
```

#### Go

```go
package example

import (
	"fmt"

	"github.com/EasyPost/easypost-go/v5"
)

func retrieve() {
	client := easypost.New("EASYPOST_API_KEY")

	// Retrieve all API keys including children
	apiKeys, _ := client.GetAPIKeys()

	fmt.Println(apiKeys)

	// Retrieve API keys for a specific child user
	childApiKeys, _ := client.GetAPIKeysForUser("user_...")

	fmt.Println(childApiKeys)
}
```

#### Java

```java
package api_keys;

import com.easypost.exception.EasyPostException;
import com.easypost.model.ApiKeys;
import com.easypost.service.EasyPostClient;

public class Retrieve {
    public static void main(String[] args) throws EasyPostException {
        EasyPostClient client = new EasyPostClient("EASYPOST_API_KEY");

        // Retrieve all API keys including children
        ApiKeys parentKeys = client.apiKey.all();

        System.out.println(parentKeys);

        // Retrieve API keys for a specific child user
        ApiKeys childKeys = client.apiKey.retrieveApiKeysForUser("user_...");

        System.out.println(childKeys);
    }
}
```

#### C#

```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost;
using Newtonsoft.Json;

namespace EasyPostExamples
{
    public class Examples
    {
        public static async Task Main()
        {
            var client = new EasyPost.Client(new EasyPost.ClientConfiguration("EASYPOST_API_KEY"));

            // Retrieve all API keys including children
            List<EasyPost.Models.API.ApiKey> apiKeys = await client.ApiKey.All();

            Console.WriteLine(JsonConvert.SerializeObject(apiKeys, Formatting.Indented));

            // Retrieve API keys for a specific child user
            List<EasyPost.Models.API.ApiKey> childApiKeys = await client.ApiKey.RetrieveApiKeysForUser("user_...");

            Console.WriteLine(JsonConvert.SerializeObject(childApiKeys, Formatting.Indented));
        }
    }
}
```

#### Node.js

```javascript
const EasyPostClient = require('@easypost/api');

const client = new EasyPostClient('EASYPOST_API_KEY');

(async () => {
  // Retrieve all API keys including children
  let apiKeys = await client.ApiKey.all();

  console.log(apiKeys);

  // Retrieve API keys for a specific child user
  let childApiKeys = await client.ApiKey.retrieveApiKeysForUser('user_...');

  console.log(childApiKeys);
})();
```

#### PHP

```php
<?php

$client = new \EasyPost\EasyPostClient('EASYPOST_API_KEY');

// Retrieve all API keys including children
$apiKeys = $client->apiKeys->all();

echo $apiKeys;

// Retrieve API keys for a specific child user
$childApiKeys = $client->apiKeys->retrieveApiKeysForUser('user_...');

echo $childApiKeys;
```

#### Python

```python
import easypost

client = easypost.EasyPostClient("EASYPOST_API_KEY")

# Retrieve all API keys including children
api_keys = client.api_key.all()

print(api_keys)

# Retrieve API keys for a specific child user
child_api_keys = client.api_key.retrieve_api_keys_for_user("user_...")

print(child_api_keys)
```

#### Ruby

```ruby
require 'easypost'

client = EasyPost::Client.new(api_key: 'EASYPOST_API_KEY')

# Retrieve all API keys including children
api_keys = client.api_key.all

puts api_keys

# Retrieve API keys for a specific child user
child_api_keys = client.api_key.retrieve_api_keys_for_user('user_...')

puts child_api_keys
```

Both Production and Test API Keys will be returned for a `User` and all of its children.
If the request is authenticated as a Child `User`, only the API Keys for that Child `User` will be returned.

---

**Production Only**

## Delete an API Key

### Example: DELETE /api_keys/:id

#### cURL

```shell
curl -X DELETE https://api.easypost.com/v2/api_keys/ak_... \
  -u "EASYPOST_API_KEY":
```

  **Important**: This endpoint only works for `ReferralCustomers` or to
  manage Child `User`accounts. If you feel this is applicable for your use case,
  please contact sales.

Once an `API Key` is deleted, it cannot be re-enabled, and will no longer be shown in your list of keys. Please exercise caution when deleting `API Key`.