

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

# 調用私有 API
<a name="apigateway-private-api-test-invoke-url"></a>

您只能使用 VPC 端點從 VPC 內部調用私有 API。您的私有 API 必須具有允許特定 VPC 和 VPC 端點的資源政策，才能調用您的 API。

如果您在不使用自訂網域名稱或私有 DNS 名稱的情況下叫用私有 API，且您的 APIs或網域名稱使用開頭為 的安全政策`SecurityPolicy_`，則必須將端點存取模式設定為 `BASIC`。如需詳細資訊，請參閱[端點存取模式](apigateway-security-policies.md#apigateway-security-policies-endpoint-access-mode)。

## 使用自訂網域名稱調用私有 API
<a name="apigateway-private-custom-domains-provider-invoke"></a>

若要使用自訂網域名稱調用私有 API，您的 VPC 端點需要一個與自訂網域名稱相關聯的網域名稱存取權，而自訂網域名稱需要允許 VPC 端點調用其名稱。如需詳細資訊，請參閱[API Gateway 中的 API 私有 API 的自訂網域名稱](apigateway-private-custom-domains.md)。

在您自己 AWS 帳戶 或不同的 VPC 中叫用私有自訂網域名稱之間沒有差異 AWS 帳戶。

### 使用您的自訂網域名稱
<a name="apigateway-private-custom-domains-invoke-1"></a>

在 VPC 內，您可以使用自訂網域名稱調用 API。下列範例是 curl 命令，用於調用您的私有自訂網域名稱：

```
curl https://private.example.com
```

### 使用端點特定的私有 DNS 主機名稱
<a name="apigateway-private-custom-domains-invoke-2"></a>

您可以使用自訂網域名稱和端點特定的私有 DNS 主機名稱來調用 API。

```
curl https://{{private-dns-hostname}}.execute-api.{{region}}.vpce.amazonaws.com/{{basepath}} -H 'Host:{{custom-domain-name}}'
```

下列範例是 curl 命令，可使用端點特定的私有 DNS 主機名稱調用您的自訂網域名稱：

```
curl https://vpce-123456-abc000.execute-api.us-east-2.vpce.amazonaws.com/test -H 'Host:private.example.com'
```

## 未使用自訂網域名稱調用私有 API
<a name="apigateway-private-api-invoke-without-custom-domain-name"></a>

若要在未使用自訂網域名稱的情況下調用您的私有 API，您需要識別 API 的 DNS 名稱。下列程序顯示如何尋找您的 DNS 名稱。

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

**尋找 DNS 名稱**

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)：// 開啟 Amazon VPC 主控台。

1. 在主要導覽窗格中，選擇**端點**，然後選擇您的 API Gateway 介面 VPC 端點。

1. 在**詳細資訊**窗格中，您會在**DNS 名稱**欄位中看到五個值。前三個是您 API 的公有 DNS 名稱。其他兩個是私有 DNS 名稱。

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

使用下列 [describe-vpc-endpoints](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpc-endpoints.html) 命令列出您的 DNS 值。

```
aws ec2 describe-vpc-endpoints --vpc-endpoint-ids vpce-01234567abcdef012
```

前三個是您 API 的公有 DNS 名稱。其他兩個是私有 DNS 名稱。

------

### 使用 Route53 別名調用私有 API
<a name="apigateway-private-api-route53-alias"></a>

您可以為 VPC 端點和私有 API 建立關聯或取消其關聯。如需詳細資訊，請參閱[(選用) 關聯或取消關聯 VPC 端點與私有 REST API](apigateway-private-api-create.md#associate-private-api-with-vpc-endpoint)。

為 VPC 端點與私有 API 建立關聯後，您可以使用下列基本 URL 調用 API：

```
https://{{{rest-api-id}}}-{{{vpce-id}}}.execute-api.{{{region}}}.amazonaws.com/{{{stage}}}
```

例如，若為 `test` 階段設定 `GET /pets`方法，且 REST API ID 為 `01234567ab`，VPC 端點 ID 為 `vpce-01234567abcdef012`，區域為 `us-west-2`，則您可以調用 API 做為：

```
curl -v https://01234567ab-vpce-01234567abcdef012.execute-api.us-west-2.amazonaws.com/test/pets
```

### 使用私有 DNS 名稱調用私有 API
<a name="w2aac15c20c17c17b9b9"></a>

如果已啟用私有 DNS，您可以使用下列私有 DNS 名稱來存取您的私有 API：

```
{{{restapi-id}}}.execute-api.{{{region}}}.amazonaws.com
```

呼叫 API 的基本 URL 格式如下：

```
https://{{{restapi-id}}}.execute-api.{{{region}}}.amazonaws.com/{{{stage}}}
```

例如，若為 `test` 階段設定 `GET /pets` 方法，而您的 REST API ID 是 `01234567ab`，區域是 `us-west-2`，則您可以在瀏覽器中輸入下列 URL 來調用您的私有 API：

```
https://01234567ab.execute-api.us-west-2.amazonaws.com/test/pets
```

或者，您可以使用下列 cURL 命令來調用私有 API：

```
curl -X GET https://01234567ab.execute-api.us-west-2.amazonaws.com/test/pets 
```

**警告**  
如果為 VPC 端點啟用私有 DNS，則無法存取公有 API 的預設端點。如需詳細資訊，請參閱[為什麼我無法從 API Gateway VPC 端點連接到我的公有 API？](https://repost.aws/knowledge-center/api-gateway-vpc-connections)。

### 使用 叫用私有 API Direct Connect
<a name="w2aac15c20c17c17b9c11"></a>

您可以使用 Direct Connect 建立從內部部署網路到 Amazon VPC 的專用私有連線，並使用公有 DNS 名稱透過該連線存取您的私有 API 端點。

您也可以透過設定 Amazon Route 53 Resolver 傳入端點並從遠端網路轉送私有 DNS 的所有 DNS 查詢，使用私有 DNS 名稱從內部部署網路存取私有 API。如需詳細資訊，請參閱《Amazon Route 53 開發人員指南》**中的[將傳入 DNS 查詢轉送到您的 VPC](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/resolver-forwarding-inbound-queries.html)。

### 使用端點特定的公有 DNS 主機名稱調用私有 API
<a name="apigateway-private-api-public-dns"></a>

您可以使用端點特定 DNS 主機名稱存取您的私有 API。這些公有 DNS 主機名稱包含 VPC 端點 ID 或您私有 API 的 API ID。

產生的基本 URL 格式如下：

```
https://{{{public-dns-hostname}}}.execute-api.{{{region}}}.vpce.amazonaws.com/{{{stage}}}
```

例如，若為 `test` 階段設定 `GET /pets` 方法，且您的 REST API ID 為 `abc1234`，其公有 DNS 主機名稱為 `vpce-def-01234567`，區域為 `us-west-2`，則您可以在 cURL 命令中使用 `Host` 標頭，使用其 VPCe ID 來調用您的私有 API：

```
curl -v https://vpce-def-01234567.execute-api.us-west-2.vpce.amazonaws.com/test/pets -H 'Host: abc1234.execute-api.us-west-2.amazonaws.com'
```

或者，您可以使用以下格式的 cURL 命令中 `x-apigw-api-id` 標頭，並透過其 API ID 調用您的私有 API：

```
curl -v https://{{{public-dns-hostname}}}.execute-api.{{{region}}}.vpce.amazonaws.com/{{{stage}}} -H 'x-apigw-api-id:{{{api-id}}}'
```