

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 API Gateway 中設定 REST API 的 API 金鑰
<a name="api-gateway-setup-api-keys"></a>

若要設定 API 金鑰，請執行下列步驟：
+ 設定 API 方法以要求 API 金鑰。
+ 為區域中的 API 建立或匯入 API 金鑰。

在設定 API 金鑰之前，您必須已建立 API 並將它部署至階段。在您建立 API 金鑰值之後便無法變更。

如需如何使用 API Gateway 主控台建立和部署 API 的說明，請參閱[在 APIs 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. 在以下網址登入 API Gateway 主控台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 選擇 REST API。

1. 在 API Gateway 主導覽窗格中，選擇 **Resources** (資源)。

1. 在 **Resources** (資源) 下，建立新的方法或選擇現有的方法。

1. 在**方法請求**索引標籤的**方法請求設定**下，選擇**編輯**。  
![\[將 API 金鑰新增至方法\]](http://docs.aws.amazon.com/zh_tw/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. 在以下網址登入 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_tw/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. 在以下網址登入 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)。