Getting Started with Endpoints on Compute Engine

This tutorial shows you how to configure and deploy a sample API and the Extensible Service Proxy (ESP) running on an instance in Google Compute Engine. 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. See Before you begin.
  2. Create a Compute Engine VM instance. See Creating a Compute Engine instance.
  3. Download the sample code. See Getting the sample code.
  4. Configure the openapi.yaml file, which is used to configure Endpoints. See Configuring Endpoints.
  5. Deploy the Endpoints configuration to create a Cloud Endpoints service. See Deploying the Endpoints configuration.
  6. Deploy the API and ESP on the Compute Engine VM. See Deploying the API backend.
  7. Send a request to the API via IP address. See Sending a request via IP address.
  8. Configure a DNS record for the sample API. See Configuring DNS for Endpoints.
  9. Send a request to the API using the domain name. See Sending a request via FQDN.
  10. Track API activity. See Tracking API activity.
  11. 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. Install cURL for testing purposes. On Windows, this tutorial uses PowerShell's built in WebRequest support.
  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. 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.

Creating a Compute Engine instance

    To create a Compute Engine instance:

  1. In the GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click Create instance.
  3. In the Boot disk section, click Change to begin configuring your boot disk.
  4. In the OS images tab, choose Debian 8.
  5. Click Select.
  6. In the Firewall section, select Allow HTTP traffic and Allow HTTPS traffic.
  7. Click Create to create the instance.
  8. Screenshot of the VM instance creation window with the required options set

    Allow a short time for the instance to start up. Once ready, it will be listed on the VM Instances page with a green status icon.

  9. Make sure you that you can connect to your VM instance.
    1. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
    2. You can now use the terminal to run Linux commands on your Debian instance. When you are done, enter exit to disconnect from the instance.
  10. Note the instance Name, Zone, and External IP address because you'll need them later.

Downloading the sample code

Download the sample code to your local machine.

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 contains the openapi.yaml configuration file based on OpenAPI Specification v2.0. Edit and deploy this file on your local machine.

To configure Endpoints:

  1. In the sample code directory, open the openapi.yaml configuration file.

    Note the following:

    • The configuration sample displays the lines near the host field, which you need to modify. To deploy openapi.yaml to Cloud Endpoints, the complete OpenAPI document is required.
    • The example openapi.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.

  2. In the host field, replace the text with the Cloud Endpoints service name, which should be in the following format:
    host: "echo-api.endpoints.[YOUR_PROJECT_ID].cloud.goog"
    

    Replace [YOUR_PROJECT_ID] with your project ID. Do not include the square brackets. For example:

    host: "echo-api.endpoints.example-project-12345.cloud.goog"
    

Note that echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog is the Endpoints service name. It is not the fully qualified domain name (FQDN) that you use for sending requests to the API.

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

After you have finished all the following configuration steps such that you can successfully send requests to the sample API using an IP address, see Configuring Endpoints DNS for information on how to configure echo-api.endpoints.YOUR-PROJECT-ID.cloud.goog to be the FQDN.

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

The first time you deploy, a new Cloud Endpoints service is created with the name that you specified in the host field of the openapi.yaml file. The service is configured according to the settings in the openapi.yaml file. When you make changes to openapi.yaml, you must redeploy the file to update the Cloud Endpoints service.

As the service is being configured, you will see a great deal of information on the terminal. You can safely ignore the warnings about the paths in openapi.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-13r0] uploaded for service [echo-api.endpoints.example-project-12345.cloud.goog]

In the example above, 2017-02-13r0 is the service configuration ID. The service configuration ID consists of a date stamp followed by a revision number. If you deploy openapi.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 additional information.

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 configuring the VM instance with metadata that ESP requires, and then deploying the API and ESP.

Configuring metadata on the VM instance

You need to configure metadata on the VM instance to provide ESP with the service name and an option that configures ESP to use the latest deployed Endpoints service configuration. With this information, ESP can obtain your API's Endpoints configuration, which allows ESP to proxy requests and responses so that Cloud Endpoints can manage your API.

  1. Get the service name of your API. This is the name that you specified in the host field of your OpenAPI document.

  2. Follow the steps to update metadata on a running instance and create the following key-value pairs:

    Key Value
    endpoints-service-name The service name of your API
    endpoints-rollout-strategy managed

    For example:

    gcloud compute instances add-metadata instance-1 \
      --metadata endpoints-service-name=echo-api.endpoints.example-project-12345.cloud.goog,endpoints-rollout-strategy=managed
    

    The endpoints-rollout-strategy:managed key-value pair 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.

Running the sample API

Java
To install and run the sample API on the instance:
  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR-INSTANCE-ZONE]
    
    Replace [YOUR-INSTANCE-ZONE] with the zone where your instance is running. Do not include the square brackets.
  2. In the sample API directory java-docs-samples/endpoints/getting-started, copy the sample API files from your local machine to the Compute Engine instance:
    gcloud compute scp --scp-flag=-pr ../../endpoints [INSTANCE-NAME]:~/
    gcloud compute scp ../../pom.xml [INSTANCE-NAME]:~/
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets. You may be prompted to create an SSH key in order to do the copy. If so, follow the prompts to create the key.
  3. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE-NAME]
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.
  4. Run the sample API. The sample API requires OpenJDK 8 and Maven. Run the following commands to get the correct versions of these packages and install them on your Compute Engine instance:
    sudo apt-get update
    sudo apt-get install openjdk-8-jdk maven
    cd endpoints/getting-started
    mvn -Djetty.http.port=8081 jetty:run
    
  5. The sample API is now running on the instance. However, you need to run the Extensible Service Proxy before sending requests to the API.
Python
To install and run the sample API on the instance:
  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR-INSTANCE-ZONE]
    
    Replace [YOUR-INSTANCE-ZONE] with the zone where your instance is running. Do not include the square brackets.
  2. In the sample API directory python-docs-samples/endpoints/getting-started, copy the sample API files from your local machine to the Compute Engine instance:
    gcloud compute scp --recurse . [INSTANCE-NAME]:~/
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets. You may be prompted to create an SSH key in order to do the copy. If so, follow the prompts to create the key.
  3. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE-NAME]
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.
  4. Depending on the Machine type that you selected when you created your VM instance, you might need to install pip. Enter the following command to see if pip is installed:
    which pip
    

    If pip is not found, enter the following command to install it:

    sudo apt-get install python-pip
    
  5. Set up a virtual environment in your instance and run the sample API.
    sudo pip install virtualenv
    virtualenv .
    ./bin/pip install -r requirements.txt
    ./bin/gunicorn -b :8081 main:app
    
  6. The sample API is now running on the instance. However, you need to run the Extensible Service Proxy before sending requests to the API.
Go
To install and run the sample API on the instance:
  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR-INSTANCE-ZONE]
    
    Replace [YOUR-INSTANCE-ZONE] with the zone where your instance is running. Do not include the square brackets.
  2. In the sample API directory golang-samples/endpoints/getting-started, copy the sample API files from your local machine to the Compute Engine instance:
    gcloud compute scp --recurse . [INSTANCE-NAME]:~/
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets. You may be prompted to create an SSH key in order to do the copy. If so, follow the prompts to create the key.
  3. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE-NAME]
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.
  4. Run the sample API.
    sudo apt-get install git -y
    curl https://storage.googleapis.com/golang/go1.7.3.linux-amd64.tar.gz | tar xzf -
    GOPATH=$HOME GOROOT=$HOME/go go/bin/go get ./...
    export PORT=8081
    GOPATH=$HOME GOROOT=$HOME/go go/bin/go run app.go
    
  5. The sample API is now running on the instance. However, you need to run the Extensible Service Proxy before sending requests to the API.
PHP
To install and run the sample API on the instance:
  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR-INSTANCE-ZONE]
    
    Replace [YOUR-INSTANCE-ZONE] with the zone where your instance is running. Do not include the square brackets.
  2. In the sample API directory php-docs-samples/endpoints/getting-started, copy the sample API files from your local machine to the Compute Engine instance:
    gcloud compute scp --recurse . [INSTANCE-NAME]:~/
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets. You may be prompted to create an SSH key in order to do the copy. If so, follow the prompts to create the key.
  3. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE-NAME]
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.
  4. Run the sample API.
    sudo apt-get install php5-cli
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
    composer install
    php -S 127.0.0.1:8081
    
  5. The sample API is now running on the instance. However, you need to run the Extensible Service Proxy before sending requests to the API.
Ruby
To install and run the sample API on the instance:
  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR-INSTANCE-ZONE]
    
    Replace [YOUR-INSTANCE-ZONE] with the zone where your instance is running. Do not include the square brackets.
  2. In the sample API directory ruby-docs-samples/endpoints/getting-started, copy the sample API files from your local machine to the Compute Engine instance:
    gcloud compute scp --recurse . [INSTANCE-NAME]:~/
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets. You may be prompted to create an SSH key in order to do the copy. If so, follow the prompts to create the key.
  3. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE-NAME]
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.
  4. Run the sample API.
    sudo apt-get install build-essential ruby ruby-dev
    sudo gem install bundler
    bundle install
    bundle exec ruby app.rb -p 8081
    
  5. The sample API is now running on the instance. However, you need to run the Extensible Service Proxy before sending requests to the API.
NodeJS
To install and run the sample API on the instance:
  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR-INSTANCE-ZONE]
    
    Replace [YOUR-INSTANCE-ZONE] with the zone where your instance is running. Do not include the square brackets.
  2. In the sample API directory nodejs-docs-samples/endpoints/getting-started, copy the sample API files from your local machine to the Compute Engine instance:
    gcloud compute scp --recurse . [INSTANCE-NAME]:~/
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets. You may be prompted to create an SSH key in order to do the copy. If so, follow the prompts to create the key.
  3. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE-NAME]
    
    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.
  4. Run the sample API. If npm is not installed on your instance, you can install it using the system's package manager. Instructions on installing npm using package managers can be found on the NodeJS website.
    npm install
    export PORT=8081
    npm start
    
  5. The sample API is now running on the instance. However, you need to run the Extensible Service Proxy before sending requests to the API.

Running the Extensible Service Proxy

The Extensible Service Proxy is an NGINX-based proxy that sits in front of your backend code. It processes incoming traffic to provide auth, API Key management, logging, and other Endpoints API Management features.

To install and run Extensible Service Proxy:

  1. Open a new terminal window, and connect to your instance using the following command:

    gcloud compute ssh [INSTANCE-NAME]
    

    Replace [INSTANCE-NAME] with your instance name. Do not include the square brackets.

  2. Install Google's Endpoints apt repository:

    1. Create an environment variable for the required distribution:

      export CLOUD_ENDPOINTS_REPO="google-cloud-endpoints-jessie"
      
    2. Add the Cloud SDK distribution URI as a package source:

      echo "deb http://packages.cloud.google.com/apt $CLOUD_ENDPOINTS_REPO main" | sudo tee /etc/apt/sources.list.d/google-cloud-endpoints.list
      
    3. Import the Google Cloud public key:

      curl --silent https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      
    4. Update and install the Cloud SDK:

      sudo apt-get update && sudo apt-get install google-cloud-sdk
      
  3. Install the Extensible Service Proxy from the apt repository:

    sudo apt-get install endpoints-runtime
    
  4. Edit the file /etc/default/nginx as root to set the listening port to 80, as follows:

    PORT=80
    

    Because ESP will be listening on port 80, make sure that you do not have any other applications using that port.

  5. Save your changes to /etc/default/nginx.

  6. Start the Extensible Service Proxy by running the command:

    sudo service nginx restart
    

    If you get an error message, see Troubleshooting Cloud Endpoints on Compute Engine.

    To confirm that ESP started properly, confirm that the nginx process is running:

    pgrep --list-full nginx
    

See Deploying the API Backend for additional information.

Sending a request via IP address

Once the sample API and the Extensible Service Proxy are running on the Compute Engine instance, you can send requests to the API.

To send a request to the API:

  1. 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, then paste it into the following environment variable statement:

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

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

    Replace [IP_ADDRESS] with the external IP address of your instance. Do not include the square brackets.

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

{
  "message": "hello world"
}

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

Configuring DNS for Endpoints

Because the Cloud Endpoints service name for the API is in the .endpoints.PROJECT-ID.cloud.goog domain, you can use it as the fully qualified domain name (FQDN) by making a small configuration change in your openapi.yaml file. This way, you can send requests to the sample API using echo-api.endpoints.YOUR-PROJECT-ID.cloud.goog instead of the IP address.

To configure Endpoints DNS:

  1. Open your OpenAPI configuration file, openapi.yaml, and add the x-google-endpoints property at the top level of the file (not indented or nested) as shown in the following snippet:
        host: "echo-api.endpoints.[YOUR_PROJECT_ID].cloud.goog"
        x-google-endpoints:
        - name: "echo-api.endpoints.[YOUR_PROJECT_ID].cloud.goog"
          target: "[IP_ADDRESS]"
    
  2. In the name property, replace [YOUR_PROJECT_ID] with your project ID. Do not include the square brackets.
  3. In the target property, replace [IP_ADDRESS] with the IP address that you used when you sent a request to the sample API. Do not include the square brackets.
  4. Deploy your updated OpenAPI configuration file to Service Management using the following command:
        gcloud endpoints services deploy openapi.yaml
    

For example, assume openapi.yaml has the following configured:

    host: "echo-api.endpoints.example-project-12345.cloud.goog"
    x-google-endpoints:
    - name: "echo-api.endpoints.example-project-12345.cloud.goog"
      target: "192.0.2.1"

When you deploy the openapi.yaml using the above gcloud command, Service Management creates a DNS A-record, echo-api.endpoints.my-project-id.cloud.goog, which resolves to the target IP address, 192.0.2.1. Note that it could take a few minutes for the new DNS configuration to propagate.

Configuring SSL

For more details on how to configure DNS & SSL, see Enabling SSL for Cloud Endpoints.

Sending a request via FQDN

Now that you've got the DNS record configured for the sample API, send a request to it using the FQDN (replace [YOUR_PROJECT_ID] with your project ID) and the ENDPOINTS_KEY environment variable set previously:
  • In Linux or Mac OS:
            curl --request POST \
                --header "content-type:application/json" \
                --data '{"message":"hello world"}' \
                "http://echo-api.endpoints.[YOUR_PROJECT_ID].cloud.goog:80/echo?key=${ENDPOINTS_KEY}"
  • In Windows PowerShell:
    (Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' -Headers @{"content-type"="application/json"} -URI "http://echo-api.endpoints.[YOUR_PROJECT_ID]:80/echo?key=$Env:ENDPOINTS_KEY").Content

You have deployed and tested an API in Cloud Endpoints!

Tracking API activity

To track API activity:

  1. Look at 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:

  1. Delete the API:
    gcloud endpoints services delete [SERVICE_NAME]
    

    Replace [SERVICE_NAME] with the name of your service.

  2. In the GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  3. Click the checkbox next to the instance you want to delete.
  4. Click the Delete button at the top of the page to delete the instance.

What's next

Send feedback about...

Cloud Endpoints with OpenAPI