

# 在 API Gateway 中为 REST API 设置 API 密钥
<a name="api-gateway-setup-api-keys"></a>

要设置 API 键，请执行以下操作：
+ 将 API 方法配置为需要 API 键。
+ 为区域中的 API 创建或导入 API 密钥。

在设置 API 键之前，您必须先创建一个 API 并将其部署到某个阶段。创建 API 密钥值后，无法更改此值。

有关如何使用 API Gateway 控制台创建和部署 API 的说明，请分别参阅[开发 API Gateway 中的 REST API](rest-api-develop.md)和[在 API Gateway 中部署 REST API。](how-to-deploy-api.md)。

创建 API 密钥后，必须将其与使用计划相关联。有关更多信息，请参阅 [在 API Gateway 中为 REST API 设置使用计划](api-gateway-create-usage-plans.md)。

**注意**  
有关可考虑的最佳实践，请参阅 [API 密钥和使用计划的最佳实践](api-gateway-api-usage-plans.md#apigateway-usage-plans-best-practices)。

**Topics**
+ [要求方法使用 API](#api-gateway-usage-plan-configure-apikey-on-method)
+ [创建 API 密钥](#api-gateway-usage-plan-create-apikey)
+ [导入 API 密钥](#api-gateway-usage-pan-import-apikey)

## 要求方法使用 API
<a name="api-gateway-usage-plan-configure-apikey-on-method"></a>

以下过程介绍如何将 API 方法配置为需要 API 密钥。

------
#### [ AWS 管理控制台 ]

**将 API 方法配置为需要 API 密钥**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 选择一个 REST API。

1. 在 API Gateway 主导航窗格中，选择**资源**。

1. 在**资源**下，创建新方法或选择现有方法。

1. 在**方法请求**选项卡上的**方法请求设置**下，选择**编辑**。  
![\[将 API 密钥添加到方法\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/api-gateway-new-console-add-key-to-method.png)

1. 选择**需要 API 密钥**。

1. 选择**保存**。

1. 部署或重新部署 API 以使该要求生效。

如果**需要 API 密钥**选项设置为 `false` 并且您没有执行上述步骤，则与 API 阶段关联的任何 API 密钥都不会用于该方法。

------
#### [ AWS CLI ]

以下 [put-method](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-method.html) 命令创建需要 API 密钥的 `PUT` 方法：

```
aws apigateway put-method \
    --rest-api-id 1234123412 \
    --resource-id a1b2c3 \
    --http-method PUT \
    --authorization-type "NONE" \
    --api-key-required
```

以下 [update-method](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-method.html) 命令将现有方法更新为需要 API 密钥：

```
aws apigateway update-method \
    --rest-api-id 1234123412 \
    --resource-id a1b2c3 \
    --http-method PUT \
    --patch-operations op="replace",path="/apiKeyRequired",value="true"
```

------
#### [ REST API ]

要让某个方法使用 API，请执行以下操作之一：
+ 调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_PutMethod.html](https://docs.aws.amazon.com/apigateway/latest/api/API_PutMethod.html) 以创建方法。在请求负载中将 `apiKeyRequired` 设置为 `true`。
+ 调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_UpdateMethod.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UpdateMethod.html) 将 `apiKeyRequired` 设置为 `true`。

------

## 创建 API 密钥
<a name="api-gateway-usage-plan-create-apikey"></a>

以下过程说明如何创建 API 密钥。如果您想要导入 API 密钥，请跳过此步骤。

------
#### [ AWS 管理控制台 ]

**创建 API 密钥**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 选择一个 REST API。

1. 在 API Gateway 主导航窗格中，选择 **API 密钥**。

1. 选择**创建 API 密钥**。  
![\[为使用计划创建 API 密钥\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/api-gateway-new-console-usage-plan-keys-choose-create-api-key-from-actions-menu.png)

1. 对于**名称**，输入名称。

1. （可选）对于**描述**，输入描述。

1. 对于 **API 密钥**，选择**自动生成**可让 API Gateway 生成密钥值，或者选择**自定义**以创建您自己的密钥值。

1. 选择**保存**。

------
#### [ AWS CLI ]

以下 [create-api-key](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-api-key.html) 命令创建 API 密钥：

```
 aws apigateway create-api-key \
    --name 'Dev API key' \
    --description 'API key for Devs' \
    --enabled
```

------
#### [ REST API ]

调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_CreateApiKey.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateApiKey.html) 以创建 API 密钥。

------

## 导入 API 密钥
<a name="api-gateway-usage-pan-import-apikey"></a>

以下过程介绍如何导入 API 密钥。如果您已创建 API 密钥，请跳过此步骤。

------
#### [ AWS 管理控制台 ]

**导入 API 密钥**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 选择一个 REST API。

1. 在主导航窗格中，选择 **API 密钥**。

1. 选择**操作**下拉菜单，然后选择**导入 API 密钥**。

1. 要加载逗号分隔的密钥文件，请选择**选择文件**。您也可以在文本编辑器中输入密钥。有关文件格式的信息，请参阅 [API Gateway API 密钥文件格式](api-key-file-format.md)。

1. 选择**收到警告停止**以在出现错误时停止导入，或选择**忽略警告**以在出现警告时继续导入有效的密钥条目。

1. 选择**导入**以导入您的 API 密钥。

------
#### [ AWS CLI ]

以下 [import-api-keys](https://docs.aws.amazon.com/cli/latest/reference/apigateway/import-api-keys.html) 命令导入 API 密钥：

```
aws apigateway import-api-key \
    a--body fileb://keys.csv \
    --format csv
```

------
#### [ REST API ]

调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_ImportApiKeys.html](https://docs.aws.amazon.com/apigateway/latest/api/API_ImportApiKeys.html) 以从某个文件导入 API 密钥。有关文件格式的信息，请参阅 [API Gateway API 密钥文件格式](api-key-file-format.md)。

------

您无法更改新 API 密钥的值。创建 API 后，您将配置使用计划。有关更多信息，请参阅 [在 API Gateway 中为 REST API 设置使用计划](api-gateway-create-usage-plans.md)。