Deploy an API managed by Cloud Endpoints

This QuickStart walks you through deploying a sample API, which Cloud Endpoints manages. The sample code includes:

  • A REST API that you can query to find the name of an airport from its three-letter IATA code.
  • A script that uploads the API configuration to Endpoints.
  • A script that deploys an App Engine flexible environment backend to host the sample API.

After you send requests to the sample API, you can view the Endpoints activity graphs and Google Cloud Observability logs in the Google Cloud console. These tools let you monitor your APIs and gain insights into their usage.

This QuickStart uses scripts to simplify the configuration steps for you to quickly see the activity graphs and logs in action. To learn how to configure and deploy a sample API, choose a tutorial for one of the API frameworks:

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  7. Verify that billing is enabled for your Google Cloud project.

Required roles

To get the permissions that you need to deploy and manage an API, ask your administrator to grant you the following IAM roles on your project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Starting Cloud Shell

  1. In the Google Cloud console, make sure you are in the project that you want to use for the sample API.

  2. Open Cloud Shell.

    Open Cloud Shell

    A Cloud Shell session opens inside a new frame at the bottom of the Google Cloud console and displays a command-line prompt. It can take a few seconds for the session to initialize.

    Cloud Shell session

  3. If you're using an existing project, make sure you have the latest version of all installed gcloud components:

    gcloud components update
    

Getting the sample code

  1. In Cloud Shell, enter the following command to get the sample API and scripts:
git clone https://github.com/GoogleCloudPlatform/endpoints-quickstart
  1. Change to the directory that contains the sample code:
cd endpoints-quickstart

Deploying the Endpoints configuration

To publish a REST API to Endpoints, an OpenAPI configuration file that describes the API is required. The sample API comes with a pre-configured OpenAPI file called openapi.yaml.

Endpoints uses Service Management, an infrastructure service of Google Cloud to create and manage APIs and services. To use Endpoints to manage an API, you deploy the API's OpenAPI configuration file to Service Management.

To deploy the Endpoints configuration:

  1. In Cloud Shell, in the endpoints-quickstart directory, enter the following:
cd scripts
  1. Run the following script, which is included in the sample:
./deploy_api.sh

Endpoints uses the host field in the OpenAPI configuration file to identify the service. The deploy_api.sh script sets the ID of your Google Cloud project as part of the name configured in the host field. When you prepare an OpenAPI configuration file for your own service, you need to do this manually.

The script then deploys the OpenAPI configuration to Service Management by using the command: gcloud endpoints services deploy openapi.yaml

As it is creating and configuring the service, Service Management outputs information to the Google Cloud console. You can safely ignore the warnings about the paths in openapi.yaml not requiring an API key. On successful completion, you see a line similar to the following that displays the service configuration ID and the service name:

    Service Configuration [2017-02-13-r2] uploaded for service [airports-api.endpoints.example-project.cloud.goog]

Enabling required services

At a minimum, Endpoints requires the following Google services to be enabled:

Name Title
servicecontrol.googleapis.com Service Control API
servicemanagement.googleapis.com Service Management API

In most cases, deploying the Endpoints configuration enables these required services.

Use the following command to confirm that the required services are enabled:

gcloud services list

If you don't see the required services listed, enable them:

gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com

Also enable your Endpoints service:

gcloud services enable YOUR-PROJECT-ID.appspot.com

For more information about the gcloud commands, see gcloud services.

Deploying the API backend

So far you have deployed the OpenAPI configuration to Service Management, but you haven't yet deployed the code to serve the API backend. The deploy_app.sh script included in the sample creates an App Engine flexible environment to host the API backend, and then the script deploys the API to App Engine.

To deploy the API backend:

  • In Cloud Shell, in the endpoints-quickstart/scripts directory, run the following script:
./deploy_app.sh

The script runs the following command to create an App Engine flexible environment in the us-central region: gcloud app create --region="$REGION"

It takes several minutes to create the App Engine flexible environment backend. After the application is created, the output is:

Success! The app is now created.

Next, the script runs the gcloud app deploy command to deploy the sample API to App Engine.

The output is: