Getting Started with Endpoints on App Engine Flexible Environment

This tutorial shows you how to configure, deploy, and send requests to a sample API 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 the request 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 and install required software. 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. Use the Cloud Platform Console to create a new Cloud Platform project, create an App Engine application, and enable billing:

    Go to App Engine

    When prompted, select the region where you want your App Engine application located and then enable billing.

  2. Note the ID of your project, because you'll need it later.

  3. Install cURL for testing purposes. On Windows, this tutorial uses PowerShell's built in WebRequest support.

  4. To deploy your app with the gcloud command-line tool, you must download, install, and initialize the Google Cloud SDK:

    Download the SDK

  5. 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.

  6. Set the default project to your project ID.

    gcloud config set project [YOUR-PROJECT-ID]
    

    Replace [YOUR-PROJECT-ID] with your project ID. Do not include the square brackets. If you have other Cloud Platform projects, and you want to use gcloud to manage them, see Managing Cloud SDK Configurations.

Getting the sample code

Java

To clone or download the sample API:

  1. 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.

  2. 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.

  2. On the line with the host field, replace YOUR-PROJECT-ID with your Cloud project ID. For example:
    host: "example-project.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.

Note: The configuration sample above displays lines near the host field, which you need to modify. To run Cloud Endpoints, the complete configuration file is required.

Deploying the Endpoints configuration

To deploy the Endpoints configuration, you use Google Service Management, an infrastructure service of Google Cloud Platform 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 service-management 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 configuration file. 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-r2] uploaded for service [example-project.appspot.com]
    

Deploying the API backend

So far you have deployed the OpenAPI configuration 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 to App Engine.

To deploy the API backend:

  1. Display the service configuration ID, by entering the following:
    gcloud service-management configs list --service=[YOUR-PROJECT-ID].appspot.com
    
    In the above command, replace [YOUR-PROJECT-ID] with your project ID. Do not include the square brackets. For example:
    gcloud service-management configs list --service=example-project.appspot.com
    
  2. In the sample code directory, open app.yaml. (If you are using the Java version of the sample, the location of app.yaml is: java-docs-samples/endpoints/getting-started/src/main/appengine)
  3. In the following section:
    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud service-management deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      config_id: ENDPOINTS-CONFIG-ID
    
    • Replace ENDPOINTS-SERVICE-NAME with your Endpoints service name.
    • Replace ENDPOINTS-CONFIG-ID with the service configuration ID displayed in the first step. For example:
    endpoints_api_service:
      name: "example-project.appspot.com"
      config_id: "2017-02-13-r2"
    
  4. Save the app.yaml file.
  5. To deploy the sample API to App Engine, run the following the command from the directory where your configuration file is located:
    Java
    1. Make sure you are in the java-docs-samples/endpoints/getting-started directory.
    2. Deploy using Maven:
      mvn appengine:deploy
    Python
    gcloud app deploy
    Go
    gcloud app deploy
    PHP
    gcloud app deploy
    Ruby
    gcloud app deploy
    NodeJS
    gcloud app deploy

Note: Anytime you make changes to your OpenAPI configuration file, you will need to update the service configuration ID in the app.yaml file and redeploy it to App Engine.

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. Do not include the square brackets.

  2. Create an API key.

    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:

    • In Linux or Mac OS:
      curl -d '{"message":"hello world"}' -H "content-type:application/json" "${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

Note: App Engine may take a few minutes to respond successfully to requests. If you send a request and get back an HTTP 502, 503, or some other server error, wait a minute and try the request again.

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

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

Send feedback about...

Cloud Endpoints with OpenAPI