

# Mock integrations for REST APIs in API Gateway
Mock integration

Amazon API Gateway supports mock integrations for API methods. This feature enables API developers to generate API responses from API Gateway directly, without the need for an integration backend. As an API developer, you can use this feature to unblock dependent teams that need to work with an API before the project development is complete. You can also use this feature to provision a landing page for your API, which can provide an overview of and navigation to your API. For an example of such a landing page, see the integration request and response of the GET method on the root resource of the example API discussed in [Tutorial: Create a REST API by importing an example](api-gateway-create-api-from-example.md).

As an API developer, you decide how API Gateway responds to a mock integration request. For this, you configure the method's integration request and integration response to associate a response with a given status code. For a method with the mock integration to return a `200` response, configure the integration request body mapping template to return the following.

```
{"statusCode": 200}
```

Configure a `200` integration response to have the following body mapping template, for example:

```
{
    "statusCode": 200,
    "message": "Go ahead without me."
}
```

 Similarly, for the method to return, for example, a `500` error response, set up the integration request body mapping template to return the following.

```
{"statusCode": 500}
```

Set up a `500` integration response with, for example, the following mapping template: 

```
{
    "statusCode": 500,
    "message": "The invoked method is not supported on the API resource."
}
```

Alternatively, you can have a method of the mock integration return the default integration response without defining the integration request mapping template. The default integration response is the one with an undefined **HTTP status regex**. Make sure appropriate passthrough behaviors are set.

**Note**  
Mock integrations aren't intended to support large response templates. If you need them for your use case, you should consider using a Lambda integration instead.

Using an integration request mapping template, you can inject application logic to decide which mock integration response to return based on certain conditions. For example, you could use a `scope` query parameter on the incoming request to determine whether to return a successful response or an error response:

```
{
  #if( $input.params('scope') == "internal" )
    "statusCode": 200
  #else
    "statusCode": 500
  #end
}
```

This way, the method of the mock integration lets internal calls to go through while rejecting other types of calls with an error response. 



In this section, we describe how to use the API Gateway console to enable the mock integration for an API method.

**Topics**
+ [

# Enable mock integration using the API Gateway console
](how-to-mock-integration-console.md)

# Enable mock integration using the API Gateway console


You must have a method available in API Gateway. Follow the instructions in [Tutorial: Create a REST API with an HTTP non-proxy integration](api-gateway-create-api-step-by-step.md).

1. Choose an API resource and choose **Create method**.

   To create the method, do the following:

   1. For **Method type**, select a method. 

   1. For **Integration type**, select **Mock**.

   1. Choose **Create method**. 

   1. On the **Method request** tab, for **Method request settings**, choose **Edit**.

   1. Choose **URL query string parameters**. Choose **Add query string** and for **Name**, enter **scope**. This query parameter determines if the caller is internal or otherwise.

   1. Choose **Save**.

1. On the **Method response** tab, choose **Create response**, and then do the following:

   1. For **HTTP Status**, enter **500**.

   1. Choose **Save**.

1. On the **Integration request** tab, for **Integration request settings**, choose **Edit**.

1. Choose **Mapping templates**, and then do the following:

   1. Choose **Add mapping template**.

   1. For **Content type**, enter **application/json**. 

   1. For **Template body**, enter the following:

      ```
      {
        #if( $input.params('scope') == "internal" )
          "statusCode": 200
        #else
          "statusCode": 500
        #end
      }
      ```

   1. Choose **Save**.

1. On the **Integration response** tab, for the **Default - Response** choose **Edit**.

1. Choose **Mapping templates**, and then do the following:

   1. For **Content type**, enter **application/json**. 

   1. For **Template body**, enter the following:

      ```
      {
          "statusCode": 200,
          "message": "Go ahead without me"
      }
      ```

   1. Choose **Save**.

1. Choose **Create response**.

   To create a 500 response, do the following:

   1. For **HTTP status regex**, enter **5\$1d\$12\$1**. 

   1. For **Method response status**, select **500**.

   1. Choose **Save**.

   1. For **5\$1d\$12\$1 - Response**, choose **Edit**. 

   1. Choose **Mapping templates**, and then choose **Add mapping template**.

   1. For **Content type**, enter **application/json**. 

   1. For **Template body**, enter the following:

      ```
      {
          "statusCode": 500,
          "message": "The invoked method is not supported on the API resource."
      }
      ```

   1. Choose **Save**.

1.  Choose the **Test** tab. You might need to choose the right arrow button to show the tab. To test your mock integration, do the following:

   1. Enter `scope=internal` under **Query strings**. Choose **Test**. The test result shows:

      ```
      Request: /?scope=internal
      Status: 200
      Latency: 26 ms
      Response Body
      
      {
        "statusCode": 200,
        "message": "Go ahead without me"
      }
      
      Response Headers
      
      {"Content-Type":"application/json"}
      ```

   1. Enter `scope=public` under `Query strings` or leave it blank. Choose **Test**. The test result shows:

      ```
      Request: /
      Status: 500
      Latency: 16 ms
      Response Body
      
      {
        "statusCode": 500,
        "message": "The invoked method is not supported on the API resource."
      }
      
      Response Headers
      
      {"Content-Type":"application/json"}
      ```

You can also return headers in a mock integration response by first adding a header to the method response and then setting up a header mapping in the integration response. In fact, this is how the API Gateway console enables CORS support by returning CORS required headers.