

# API Gateway で REST API を開発する
<a name="rest-api-develop"></a>

 Amazon API Gateway では、REST API を、API Gateway [リソース](https://docs.aws.amazon.com/apigateway/latest/api/API_Resource.html)と呼ばれるプログラム可能なエンティティのコレクションとして構築します。たとえば、[RestApi](https://docs.aws.amazon.com/apigateway/latest/api/API_RestApi.html) リソースを使用して[リソース](https://docs.aws.amazon.com/apigateway/latest/api/API_Resource.html)エンティティのコレクションを含むことができる API を表します。

各 `Resource` エンティティは、[メソッド](https://docs.aws.amazon.com/apigateway/latest/api/API_Method.html)リソースを 1 つ以上持つことができます。`Method` は、クライアントが送信する受信リクエストであり、パスパラメータ、ヘッダー、またはクエリ文字列パラメータなどのリクエストパラメータを含めることができます。さらに、HTTP メソッドによっては、リクエストに本文を含めることができます。メソッドは、クライアントが公開された `Resource` にアクセスする方法を定義します。`Method` をバックエンドエンドポイント (統合エンドポイントとも呼ばれます) と統合するには、[統合](https://docs.aws.amazon.com/apigateway/latest/api/API_Integration.html)リソースを作成します。これにより、受信リクエストが指定先の統合エンドポイント URI に転送されます。必要に応じて、バックエンド要件を満たすようにリクエストパラメータまたはリクエスト本文を変換したり、プロキシ統合を作成したりできます。プロキシ統合では、API Gateway がリクエスト全体を標準化された形式で統合エンドポイント URI に送信して、レスポンスをクライアントに直接送信します。

レスポンスでは、[MethodResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html) を作成してクライアントが受信するレスポンスを表し、[IntegrationResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_IntegrationResponse.html) リソースを作成してバックエンドから返されるレスポンスを表すことができます。統合レスポンスを使用して、データがクライアントに返される前にバックエンドレスポンスデータを変換するか、バックエンドレスポンスをそのままクライアントにパスすることができます。

## REST API のリソース例
<a name="rest-api-develop-example"></a>

次の図は、API Gateway が HTTP プロキシと `GET /pets` リソースの HTTP 非プロキシ統合に対してこのリクエスト/レスポンスモデルを実装する方法を説明しています。クライアントは API Gateway に `x-version:beta` ヘッダーを送信し、API Gateway はクライアントに `204` ステータスコードを送信します。

非プロキシ統合では、API Gateway は、統合リクエストと統合レスポンスを変更することで、バックエンド要件を満たすデータ変換を実行します。非プロキシ統合では、メソッドリクエストで本文にアクセスできます。ただしこれは統合リクエストで変換できます。統合エンドポイントが本文を含むレスポンスを返すと、統合レスポンスでアクセスして変換します。メソッドレスポンスで本文を変更することはできません。

プロキシ統合では、統合エンドポイントがリクエストとレスポンスを変更します。API Gateway は、統合リクエストや統合レスポンスを変更することはなく、受信リクエストをそのままバックエンドに送信します。

統合タイプを問わず、クライアントは API Gateway にリクエストを送信し、API Gateway は同期的に応答しました。

------
#### [ Non-proxy integration ]

![\[API Gateway の非プロキシ統合の図\]](http://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/images/develop-non-proxy.png)


------
#### [ Proxy integration ]

![\[API Gateway のプロキシ統合の図\]](http://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/images/develop-proxy.png)


------

次の実行ログの例は、前の例で API Gateway がログに記録する内容を説明しています。分かりやすいように、値と初期ログをいくつか省いてあります。

------
#### [ Non-proxy integration ]

```
Wed Feb 12 23:56:44 UTC 2025 : Starting execution for request: abcd-1234-5678
Wed Feb 12 23:56:44 UTC 2025 : HTTP Method: GET, Resource Path: /pets
Wed Feb 12 23:56:44 UTC 2025 : Method request path: {}
Wed Feb 12 23:56:44 UTC 2025 : Method request query string: {}
Wed Feb 12 23:56:44 UTC 2025 : Method request headers: {x-version=beta}
Wed Feb 12 23:56:44 UTC 2025 : Method request body before transformations: 
Wed Feb 12 23:56:44 UTC 2025 : Endpoint request URI: http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:56:44 UTC 2025 : Endpoint request headers: {app-version=beta}
Wed Feb 12 23:56:44 UTC 2025 : Endpoint request body after transformations: 
Wed Feb 12 23:56:44 UTC 2025 : Sending request to http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:56:45 UTC 2025 : Received response. Status: 200, Integration latency: 123 ms
Wed Feb 12 23:56:45 UTC 2025 : Endpoint response headers: {Date=Wed, 12 Feb 2025 23:56:45 GMT}
Wed Feb 12 23:56:45 UTC 2025 : Endpoint response body before transformations:
Wed Feb 12 23:56:45 UTC 2025 : Method response body after transformations: (null)
Wed Feb 12 23:56:45 UTC 2025 : Method response headers: {X-Amzn-Trace-Id=Root=1-abcd-12345}
Wed Feb 12 23:56:45 UTC 2025 : Successfully completed execution
Wed Feb 12 23:56:45 UTC 2025 : Method completed with status: 204
```

------
#### [ Proxy integration ]

```
Wed Feb 12 23:59:42 UTC 2025 : Starting execution for request: abcd-1234-5678
Wed Feb 12 23:59:42 UTC 2025 : HTTP Method: GET, Resource Path: /pets
Wed Feb 12 23:59:42 UTC 2025 : Method request path: {}
Wed Feb 12 23:59:42 UTC 2025 : Method request query string: {}
Wed Feb 12 23:59:42 UTC 2025 : Method request headers: {x-version=beta}
Wed Feb 12 23:59:42 UTC 2025 : Method request body before transformations: 
Wed Feb 12 23:59:42 UTC 2025 : Endpoint request URI: http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:59:42 UTC 2025 : Endpoint request headers: { x-version=beta}
Wed Feb 12 23:59:42 UTC 2025 : Endpoint request body after transformations: 
Wed Feb 12 23:59:42 UTC 2025 : Sending request to http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:59:43 UTC 2025 : Received response. Status: 204, Integration latency: 123 ms
Wed Feb 12 23:59:43 UTC 2025 : Endpoint response headers: {Date=Wed, 12 Feb 2025 23:59:43 GMT}
Wed Feb 12 23:59:43 UTC 2025 : Endpoint response body before transformations: 
Wed Feb 12 23:59:43 UTC 2025 : Method response body after transformations:
Wed Feb 12 23:59:43 UTC 2025 : Method response headers: {Date=Wed, 12 Feb 2025 23:59:43 GMT}
Wed Feb 12 23:59:43 UTC 2025 : Successfully completed execution
Wed Feb 12 23:59:43 UTC 2025 : Method completed with status: 204
```

------

同様の API をインポートして AWS マネジメントコンソール でテストするには、[API の例](api-gateway-create-api-from-example.md) を参照してください。

## 開発のための REST API のその他の関数
<a name="rest-api-develop-details"></a>

API Gateway は、REST API の開発のためにその他の関数をサポートしています。例えば、ユーザーが API を理解しやすいように、API のドキュメントを提供できます。これを行うには、サポートされている API エンティティに [DocumentationPart](https://docs.aws.amazon.com/apigateway/latest/api/API_DocumentationPart.html) リソースを追加します。

クライアントが API を呼び出す方法を制御するには、 [IAM アクセス許可](permissions.md)、[Lambda 認証](apigateway-use-lambda-authorizer.md)、または [Amazon Cognito ユーザープール](apigateway-integrate-with-cognito.md)を使用します。API の使用を計測するには、API リクエストを調整するように[使用量プラン](api-gateway-api-usage-plans.md)を設定します。これらは、API の作成時または更新時に有効にすることができます。

次の図は、REST API 開発で使用できる関数と、リクエスト/レスポンスモデルでこれらの関数を設定する場所を示しています。

![\[API Gateway 関数の図\]](http://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/images/develop-features.png)


API の作成方法の概要については、「[チュートリアル: Lambda プロキシ統合を使用して REST API を作成する](api-gateway-create-api-as-simple-proxy-for-lambda.md)」を参照してください。REST API の開発時に使用できる API Gateway 機能の詳細については、以下のトピックを参照してください。これらのトピックには、概念情報に加えて、API Gateway コンソール、API Gateway REST API、AWS CLI、またはいずれかの AWS SDK を使用して実行できる手順が含まれています。

**Topics**
+ [REST API のリソース例](#rest-api-develop-example)
+ [開発のための REST API のその他の関数](#rest-api-develop-details)
+ [API Gateway での REST API の API エンドポイントタイプ](api-gateway-api-endpoint-types.md)
+ [API Gateway での REST API のセキュリティポリシー](apigateway-security-policies.md)
+ [API Gateway の REST API の IP アドレスタイプ](api-gateway-ip-address-type.md)
+ [API Gateway の REST API のメソッド](how-to-method-settings.md)
+ [API Gateway で REST API へのアクセスを制御および管理する](apigateway-control-access-to-api.md)
+ [API Gateway での REST API の統合](how-to-integration-settings.md)
+ [API Gateway での REST API のリクエスト検証](api-gateway-method-request-validation.md)
+ [API Gateway での REST API のデータ変換](rest-api-data-transformations.md)
+ [API Gateway での REST API のゲートウェイレスポンス](api-gateway-gatewayResponse-definition.md)
+ [API Gateway での REST API の CORS](how-to-cors.md)
+ [API Gateway での REST API のバイナリメディアタイプ](api-gateway-payload-encodings.md)
+ [API Gateway で REST API を呼び出す](how-to-call-api.md)
+ [API Gateway で OpenAPI を使用して REST API を開発する](api-gateway-import-api.md)