All Products
Search
Document Center

API Gateway:Access a VPC backend service with API Gateway

Last Updated:Aug 26, 2026

This topic describes how to use API Gateway to access a backend service in a VPC. It also explains how to use AppCode authentication from Alibaba Cloud App for secure API calls.

Prerequisites

Note
  • Purchase a Server Load Balancer (SLB) instance and an ECS instance in the VPC, and deploy your service.

  • In this example, the backend service for API Gateway is an ECS instance in a VPC. An Nginx web service is deployed on the ECS instance at port 80.

Procedure

To use API Gateway to access a backend service in a VPC, follow these steps:

Create a VPC access authorization

Create a VPC access authorization to allow API Gateway to access your VPC.

  1. Log on to the API Gateway console. Select a region and, in the left-side navigation pane, choose API Management > VPC Authorization.

  2. On the Authorization List page, click Create Authorization in the upper-right corner.

  3. On the Create VPC Authorization page, enter the VPC Authorization Name, VPC Id, Instance ID or IP Address, and Port.

    Note
    • To access a site (vhost) on an SLB or ECS instance using a specific domain name, configure the domain name in the Host field.

    • You can find the VPC ID, instance ID, or private IP address on the details page of your backend service, such as an ECS or Classic Load Balancer (CLB) instance.

Important

Changing the network type to public is not supported and will cause request failures.

Create an API group

An API group is a management unit for APIs. You must create an API group before you can create an API.

  1. Log on to the API Gateway console. Select a region and, in the left-side navigation pane, choose API Management > Group Management. Then, click Create Group in the upper-right corner.

  2. In the Create Group dialog box, select an Instance, enter nginx-demo for Group Name and / for Base Path, and then click OK.

Note
  • On the Group List page, you can view the created group. Click the group name to go to the Group Details page, where you can bind a domain name, modify basic information, or change the group instance.

  • API Gateway automatically assigns a public second-level domain name to the API group. This domain name is for debugging only and is limited to 1,000 calls per day. In regions outside the Chinese mainland, such as China (Hong Kong), the limit is 100 calls per day. For production use, bind a custom domain name to the group.

Create an API

  1. Log on to the API Gateway console. Select a region and, in the left-side navigation pane, choose API Management > Group Management.

  2. On the Group List page, find the nginx-demo group and click API Management in the Actions column.

  3. On the API List page, click Create API in the upper-right corner.

  4. On the Create API page, in the Basic Information section, configure the following parameters and click Next.

    Parameter

    Example

    Group

    nginx-demo

    API Name

    nginx-test

    Security Authentication

    Alibaba Cloud App

    AppCode Authentication

    Allow AppCode (Header & Query) Authentication

    Signature Algorithm

    HMAC_SHA256

  5. In the Define API Request section, configure the following parameters and click Next.

    Parameter

    Example

    Protocol

    HTTP, HTTPS

    Request Path

    /nginx

    HTTP Method

    GET

    Request Mode

    Pass-through

    Note

    This step defines how a client, such as a browser, a mobile app, or another business system, can send requests to your API. You need to define the request protocol, request path, HTTP method, and request mode. In this example, Request Mode is set to Pass-through, meaning API Gateway forwards client requests with their original parameters directly to the backend service in the VPC.

  6. In the Define Backend Service section, configure the following parameters and click Next.

    Parameter

    Example

    Backend Configuration

    Select Custom Backend Service

    Backend Service Type

    Select VPC

    VPC Authorization Name

    ***-microservice

    Backend Request Path

    /

    HTTP Method

    GET

    Backend Timeout

    10000

    Note

    This step defines how API Gateway maps parameters and connects to the backend service after receiving a client request. In this example, the backend service type is VPC. Select the VPC access authorization that you created or enter its name, and then specify the backend request path.

  7. In the Define Response section, click Create.

    Note

    This step generates API documentation to help callers understand the API. You can set the response Content-Type, response example, and error response example. This example does not involve these settings. Click Create.

  8. In the message that appears, click Publish.

  9. In the Publish API dialog box, set Environment to Release, enter a description in the Remarks field, and then click Publish.

    Note

    After you modify an API, you must publish it to an environment for the changes to take effect. API Gateway provides three built-in environments: Release, Pre, and Test.

Create an application and authorize the API

An application is the identity used to call an API. In this example, the authentication method is set to "Alibaba Cloud App" when you create the API. After you publish the API, you must create an application and authorize it to call the API.

Create an application

  1. Log on to the API Gateway console. Select a region and, in the left-side navigation pane, choose API Calling > Application Management.

  2. On the Applications and Authorizations page, click Create APP in the upper-right corner.

  3. In the Create Application dialog box, enter an Application Name and click OK.

    The application name can contain Chinese characters, letters, digits, and underscores (_). It must start with a letter or Chinese character and be 4 to 26 characters in length. You can also expand Tag Settings, Custom AK, or Extended Fields, and enter a Description of up to 180 characters.

  4. On the Applications and Authorizations page, click the name of the application that you created to go to the Application Details page. You can see two authentication methods for Alibaba Cloud App: AppKey and AppCode. In this example, we use AppCode for simple authentication. For more information, see Call an API by using simple authentication (AppCode).

Authorize the API

  1. Log on to the API Gateway console. Select a region and, in the left-side navigation pane, choose API Management > API List.

  2. On the API List page, find the nginx-test API. In the Actions column, click image and choose Authorize.

  3. In the Authorize panel that appears, set Select an environment to authorize to Release. Search for the application that you created, click Add, and then click OK. A success message is displayed.

Allow API Gateway egress IP in security group

If your ECS instance's security group has inbound rules that restrict traffic, you must add the API Gateway egress IP address to the security group's allowlist. This action prevents the security group from blocking requests from API Gateway. The required egress IP address belongs to the instance that hosts your API group.

  1. Log on to the API Gateway console. In the left-side navigation pane, choose API Management > Group Management, and then click the name of your target API group.

  2. On the Group Details page, view the Instance Type of the group.

  3. In the left-side navigation pane, choose Instances and Clusters. Click the instance type, find the corresponding instance, and then view the Egress IP Address information.

    The egress IP addresses include a public egress IP address and an internal VPC address.

  4. Log on to the ECS console. In the left-side navigation pane, choose Instances & Images > Instances. Click the ID of your ECS instance. Go to the Security Groups tab, and click the security group name. On the Security Group Details page, choose Inbound > Quickly Add Rules, and configure the egress IP address of the gateway.

    In the Quickly Add Rules dialog box, set Authorization Policy to Allow. Enter the gateway's egress IP address in the Authorization Object field. Select All (1/65535) for Port Range, and then click OK.

Debug the API

After configuring an API, use this feature to verify that it works as expected before calling it from a client.

Note
  • The Debug page supports request messages of up to 512 KB in size. For larger requests, use an SDK for debugging.

  • Your configured instance blacklists, whitelists, and IP address-based access control plug-ins must allow traffic from 47.245.113.207.

  • This page does not support debugging for the JWT plug-in.

  • Only public domain names are supported for debugging.

  1. Log on to the API Gateway console. In the left-side navigation pane, choose API Calling > Debug.

  2. On the Debug page, select the nginx-test API that you created. For Authentication Method, select Use AppCode. Then, click Send Request.

    If the call is successful, the response status code is 200, and the response body returns the default Nginx welcome page in HTML, which includes text such as Welcome to nginx!.

    If debugging fails, see the following topics:

Call the API

For more information, see Call an API from a client.

Note