Getting Started with Endpoints on App Engine Flexible Environment

This tutorial shows you how to configure and deploy a sample API and the Extensible Service Proxy (ESP) running on an instance in the App Engine flexible environment. The sample code's REST API is described using the OpenAPI Specification. The tutorial also shows you how to create an API key and use it in requests to the API.

For an overview of Cloud Endpoints, see About Cloud Endpoints and Cloud Endpoints Architecture.

Task List

Use the following high-level task list as you work through the tutorial. All tasks are required to successfully send requests to the API.

  1. Set up a Cloud Platform project, install required software, and create an App Engine app. See Before you begin.
  2. Download the sample code. See Getting the sample code.
  3. Configure the openapi-appengine.yaml file, which is used to configure Endpoints. See Configuring Endpoints.
  4. Deploy the Endpoints configuration to create a Cloud Endpoints service. See Deploying the Endpoints configuration.
  5. Create a backend to serve the API and deploy the API. See Deploying the API backend.
  6. Send a request to the API. See Sending a request to the API.
  7. Track API activity. See Tracking API activity.
  8. Avoid incurring charges to your Google Cloud Platform account. See Clean up.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Note the project ID, because you'll need it later.
  5. You will need an application to send requests to the sample API.

    • Linux and Mac users: This tutorial provides an example using curl, which typically comes pre-installed on your operating system. If you don't have curl, you can download it from the curl Releases and Downloads page.
    • Windows users: This tutorial provides an example using Invoke-WebRequest, which is supported in PowerShell 3.0 and later.
  6. Download the Google Cloud SDK.
  7. Update the Cloud SDK and install the Endpoints components.
    gcloud components update
  8. Make sure that Cloud SDK (gcloud) is authorized to access your data and services on Google Cloud Platform:
    gcloud auth login
    A new browser tab opens and you are prompted to choose an account.
  9. Set the default project to your project ID.
    gcloud config set project [YOUR_PROJECT_ID]

    Replace [YOUR_PROJECT_ID] with your project ID. If you have other Cloud Platform projects, and you want to use gcloud to manage them, see Managing Cloud SDK Configurations.

  10. Select the region where you want to create your App Engine app. Run the following command to get a list of regions:
    gcloud app regions list
  11. Create an App Engine app using the following command. Replace [YOUR_PROJECT_ID] with your Cloud project ID and [YOUR_REGION] with the region that you want the App Engine app created in.
      gcloud app create \
      --project=[YOUR_PROJECT_ID] \
      --region=[YOUR_REGION]
    

Getting the sample code

Java

To clone or download the sample API:

  1. The sample code uses Maven. If you don't have Maven 3.3.9 or higher, download and install it.
  2. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/java-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  3. Change to the directory that contains the sample code:
    cd java-docs-samples/endpoints/getting-started
Python

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/python-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd python-docs-samples/endpoints/getting-started
Go

To clone or download the sample API:

  1. Make sure your GOPATH environment variable is set.
  2. Clone the sample app repository to your local machine:
    go get -u -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
  3. Change to the directory that contains the sample code:
    cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
PHP

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/php-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd php-docs-samples/endpoints/getting-started
Ruby

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd ruby-docs-samples/endpoints/getting-started
NodeJS

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd nodejs-docs-samples/endpoints/getting-started

Configuring Endpoints

The sample code includes the OpenAPI configuration file, openapi-appengine.yaml, which is based on OpenAPI Specification v2.0.

To configure Endpoints:
  1. In the sample code directory, open the openapi-appengine.yaml configuration file.

    Note the following:

    • The configuration sample displays the lines near the host field, which you need to modify. To deploy openapi-appengine.yaml to Cloud Endpoints, the complete OpenAPI document is required.
    • The example openapi-appengine.yaml contains a section for configuring authentication that is not needed for this tutorial. You do not need to configure the lines with YOUR-SERVICE-ACCOUNT-EMAIL and YOUR-CLIENT-ID.
    • OpenAPI is a language-agnostic specification. The same openapi-appengine.yaml file is in the getting-started sample in each language GitHub repository for convenience.
  2. On the line with the host field, replace YOUR-PROJECT-ID with your Cloud project ID. For example:
    host: "example-project-12345.appspot.com"
    

Cloud Endpoints uses the text configured in the host field as the service name. When you deploy the API to the App Engine backend, a DNS entry with a name in the format YOUR-PROJECT-ID.appspot.com is created automatically.

For information about the fields in the OpenAPI document that Cloud Endpoints requires, see Configuring Endpoints.

Deploying the Endpoints configuration

To deploy the Endpoints configuration, you use the gcloud endpoints services deploy command. This command uses Google Service Management, an infrastructure service of GCP that manages other APIs and services, including services created using Cloud Endpoints.

To deploy the Endpoints configuration:
  1. Make sure you are in the endpoints/getting-started directory.
  2. Invoke the following command:
    gcloud endpoints services deploy openapi-appengine.yaml

    This creates a new Cloud Endpoints service with the name that you specified in the host field of the openapi-appengine.yaml file (if it does not already exist). The service is updated according to your OpenAPI document. No matter what the Endpoints service name is, when you deploy the API on App Engine, a DNS record is created using the name format PROJECT-ID.appspot.com, which is the FQDN that you use when you send requests to the API.

    As it is creating and configuring the service, Service Management outputs a great deal of information to the terminal. You can safely ignore the warnings about the paths in openapi-appengine.yaml not requiring an API key. On successful completion, you will see a line like the following that displays the service configuration ID and the service name:

    Service Configuration [2017-02-13-r0] uploaded for service [example-project-12345.appspot.com]
    

In the above example, 2017-02-13-r0 is the service configuration ID and example-project-12345.appspot.com is the service name. The service configuration ID consists of a date stamp followed by a revision number. If you deploy openapi-appengine.yaml again on the same day, the revision number is incremented in the service configuration ID.

If you get an error message, see Troubleshooting Endpoints Configuration Deployment.

See Deploying the Endpoints Configuration for more information.

Deploying the API backend

So far you have deployed the OpenAPI document to Service Management, but you have not yet deployed the code that will serve the API backend. This section walks you through deploying the sample API and ESP to App Engine.

To deploy the API backend:

  1. Add your service name to app.yaml:

    Java

    Open endpoints/getting-started/src/main/appengine/app.yaml.

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Python

    Open endpoints/getting-started/app.yaml.

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Go

    Open endpoints/getting-started/app.yaml.

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    PHP

    Open endpoints/getting-started/app.yaml.

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command. If you have
      # previously run the deploy command, you can list your existing configuration
      # ids using the 'configs list' command as follows:
      #
      #     gcloud endpoints configs list --service=YOUR-PROJECT-ID.appspot.com
      #
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Ruby

    Open endpoints/getting-started/app.yaml.

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    NodeJS

    Open endpoints/getting-started/app.yaml.

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Replace ENDPOINTS-SERVICE-NAME with the name of your Endpoints service. This is the same name that you configured in the host field of your OpenAPI document. For example:

    endpoints_api_service:
      name: example-project-12345.appspot.com
      rollout_strategy: managed
    

    The rollout_strategy: managed option configures ESP to use the latest deployed service configuration. When you specify this option, within a minute after you deploy a new service configuration, ESP detects the change and automatically begins using it. We recommend that you specify this option instead of a specific configuration ID for ESP to use.

  2. Save the app.yaml file.
  3. Because the endpoints_api_service section is included in app.yaml, the gcloud app deploy command deploys and configures ESP in a separate container to your App Engine flexible environment. All request traffic is routed through ESP, and it proxies requests and responses to and from the container running your backend server code.

  4. Make sure you are in the endpoints/getting-started directory. This is where your openapi-appengine.yaml configuration file is located.
  5. Run the following command to deploy the sample API to App Engine:
  6. Java

    Deploy using Maven:

    mvn appengine:stage
    gcloud app deploy target/appengine-staging
    
    Python
    gcloud app deploy
    Go
    gcloud app deploy
    PHP
    gcloud app deploy
    Ruby
    gcloud app deploy
    NodeJS
    gcloud app deploy

    We recommend that you wait a few minutes before sending requests to your API while App Engine completely initializes.

If you get an error message, see Troubleshooting App Engine Flexible Deployment.

For more information, see Deploying the API Backend.

Sending requests to the API

Now that the service is running on App Engine you can send requests to it.

  1. Export an environment variable for your App Engine project URL:

    • Linux Bash shell: export ENDPOINTS_HOST=https://[YOUR_PROJECT_ID].appspot.com
    • Windows Powershell: $Env:ENDPOINTS_HOST="https://[YOUR_PROJECT_ID].appspot.com"

    Replace [YOUR_PROJECT_ID] with your project ID.

  2. In the same GCP project that you used for your API, create an API key on the API credentials page. (See Enabling an API in Your Cloud Project if you want to create an API key in a different GCP project.)

    Create an API key

    1. Click Create credentials, then select API key.
    2. Copy the key to the clipboard.
    3. Click Close.
    4. On the Credentials page, click Save.
  3. Paste the API key into the following environment variable statement:

    • In Linux or Mac OS: export ENDPOINTS_KEY=AIza...
    • In Windows PowerShell: $Env:ENDPOINTS_KEY="AIza..."
  4. Send an HTTP request using curl or Invoke-WebRequest using the ENDPOINTS_HOST and ENDPOINTS_KEY environment variables set previously:

    • In Linux or Mac OS:
        curl --request POST \
            --header "content-type:application/json" \
            --data '{"message":"hello world"}' \
            "${ENDPOINTS_HOST}/echo?key=${ENDPOINTS_KEY}"
        
    • In Windows PowerShell:
      (Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' -Headers @{"content-type"="application/json"} -URI "$Env:ENDPOINTS_HOST/echo?key=$Env:ENDPOINTS_KEY").Content

The API echoes back the message that you send it, and responds with the following:

{
  "message": "hello world"
}

In the above curl:

  • The --data option specifies the data to post to the API.
  • The --header option specifies the header.
  • The query parameter key is set to the API key. The example uses the environment variable ${ENDPOINTS_KEY}. If you did not set the environment variable, you can copy/paste the API key as the value for the key query parameter. In that case, do not surround it with ${}.

If you would prefer to use an application such as Postman to send the request, you would configure it as follows:

  • Select POST as the HTTP verb.
  • For the header, select the key content-type and the value application/json.
  • For the body, enter the following:
    {"message":"hello world"}
  • In the URL, use the actual API key rather than the environment variable. For example:
    http://192.0.2.0:80/echo?key=AIzaSyBmDH4jBQhbR7yxKJ9IAq-Dmlio5Wh3rD0

If you did not get a successful response, see Troubleshooting Response Errors.

You just deployed and tested an API in Cloud Endpoints!

Tracking API activity

  1. View the activity graphs for your API in the Endpoints page.
    View Endpoints activity graphs
    It may take a few moments for the request to be reflected in the graphs.
  2. Look at the request logs for your API in the Logs Viewer page.
    View Endpoints request logs

Creating a developer portal for the API

You can use Cloud Endpoints Portal to create a developer portal, a website that you can use to interact with the sample API. To learn more, see Cloud Endpoints Portal Overview.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

See Deleting an API and API Instances for information on stopping the services used by this tutorial.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Endpoints with OpenAPI