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 Endpoints and Endpoints architecture.
Objectives
Use the following high-level task list as you work through the tutorial. All tasks are required to successfully send requests to the API.
- Set up a Google Cloud project, install the required software, and create an App Engine app. See Before you begin.
- Download the sample code. See Getting the sample code.
- Configure the
openapi-appengine.yaml
file, which is used to configure Endpoints. See Configuring Endpoints. - Deploy the Endpoints configuration to create an Endpoints service. See Deploying the Endpoints configuration.
- Deploy the API and ESP to App Engine. See Deploying the API backend.
- Send a request to the API. See Sending a request to the API.
- Track API activity. See Tracking API activity.
- Avoid incurring charges to your Google Cloud account. See Clean up.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- Make a note of the project ID because it's needed later.
-
You need an application to send requests to the sample API.
- Linux and macOS users: This tutorial provides an example of using
curl
, which typically comes pre-installed on your operating system. If you don't havecurl
, you can download it from thecurl
Releases and downloads page. - Windows users: This tutorial provides an example using
Invoke-WebRequest
, which is supported in PowerShell 3.0 and later.
- Linux and macOS users: This tutorial provides an example of using
- Download the Google Cloud CLI.
-
Update the gcloud CLI and install the Endpoints
components.
gcloud components update
-
Make sure that the Google Cloud CLI (
gcloud
) is authorized to access your data and services on Google Cloud: In the new browser tab that opens, select an account.gcloud auth login
-
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 Google Cloud projects, and you want to use
gcloud
to manage them, see Managing gcloud CLI configurations. - Select the region where you want to create your App Engine
application. Run the following command to get a list of regions:
gcloud app regions list
- Create an App Engine application.
Replace YOUR_PROJECT_ID with your Google Cloud
project ID and YOUR_REGION with the region that you
want the App Engine application created in.
gcloud app create \ --project=YOUR_PROJECT_ID \ --region=YOUR_REGION
Getting the sample code
To clone or download the sample API:
- The sample code uses Maven. If you don't have Maven 3.3.9 or higher, download and install it.
- 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.
- Change to the directory that contains the sample code:
cd java-docs-samples/endpoints/getting-started
To clone or download the sample API:
- 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.
- Change to the directory that contains the sample code:
cd python-docs-samples/endpoints/getting-started
To clone or download the sample API:
- Make sure your
GOPATH
environment variable is set. - Clone the sample app repository to your local machine:
go get -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
- Change to the directory that contains the sample code:
cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
To clone or download the sample API:
- 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.
- Change to the directory that contains the sample code:
cd php-docs-samples/endpoints/getting-started
To clone or download the sample API:
- 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.
- Change to the directory that contains the sample code:
cd ruby-docs-samples/endpoints/getting-started
To clone or download the sample API:
- 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.
- 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.
- In the sample code directory, open the
openapi-appengine.yaml
configuration file.Java Python Go PHP Ruby NodeJS Note the following:
- The configuration sample displays the lines near the
host
field, which you need to modify. To deployopenapi-appengine.yaml
to Endpoints, the complete OpenAPI document is required. - The example
openapi-appengine.yaml
contains a section for configuring authentication that isn't needed for this tutorial. You don't 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 thegetting-started
sample in each language GitHub repository for convenience.
- The configuration sample displays the lines near the
- On the line with the
host
field, replace YOUR-PROJECT-ID with your Google Cloud project ID. For example:host: "example-project-12345.appspot.com"
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 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 Service Management
to create a managed service.
To deploy the Endpoints configuration:
- Make sure you are in the
endpoints/getting-started
directory. - Upload the configuration and create a managed service:
gcloud endpoints services deploy openapi-appengine.yaml
The gcloud
command then calls the Service Management
API to create a managed service with the name that you specified in the
host
field of the openapi-appengine.yaml
file.
Service Management configures the service according to the
settings in the openapi-appengine.yaml
file. When you make changes to
openapi-appengine.yaml
, you must redeploy the file to update the
Endpoints service.
As it is creating and configuring the service, Service Management
outputs information to the terminal. You can safely ignore the warnings about
the paths in the openapi-appengine.yaml
file not requiring an API key.
When it finishes configuring the service, Service Management displays a
message with the service configuration ID and the service name, similar to the
following:
Service Configuration [2017-02-13r0] uploaded for service [example-project-12345.appspot.com]
In the preceding example, 2017-02-13r0
is the service
configuration ID, and example-project-12345.appspot.com
is the
Endpoints service. The service configuration ID consists of a
date stamp followed by a revision number. If you deploy the
openapi-appengine.yaml
file again on the same day, the revision
number is incremented in the service configuration ID. You can view
the Endpoints service configuration on the Endpoints >
Services page in the Google Cloud console.
If you get an error message, see Troubleshooting Endpoints configuration deployment.
Checking required services
At a minimum, Endpoints and ESP require the following Google services to be enabled:Name | Title |
---|---|
servicemanagement.googleapis.com |
Service Management API |
servicecontrol.googleapis.com |
Service Control API |
endpoints.googleapis.com |
Google Cloud Endpoints |
In most cases, the gcloud endpoints services deploy
command enables these
required services. However, the gcloud
command completes successfully but
doesn't enable the required services in the following circumstances:
If you used a third-party application such as Terraform, and you don't include these services.
You deployed the Endpoints configuration to an existing Google Cloud project in which these services were explicitly disabled.
Use the following command to confirm that the required services are enabled:
gcloud services list
If you do not see the required services listed, enable them:
gcloud services enable servicemanagement.googleapis.comgcloud services enable servicecontrol.googleapis.com
gcloud services enable endpoints.googleapis.com
Also enable your Endpoints service:
gcloud services enable ENDPOINTS_SERVICE_NAME
To determine the ENDPOINTS_SERVICE_NAME you can either:
After deploying the Endpoints configuration, go to the Endpoints page in the Cloud console. The list of possible ENDPOINTS_SERVICE_NAME are shown under the Service name column.
For OpenAPI, the ENDPOINTS_SERVICE_NAME is what you specified in the
host
field of your OpenAPI spec. For gRPC, the ENDPOINTS_SERVICE_NAME is what you specified in thename
field of your gRPC Endpoints configuration.
For more information about the gcloud
commands, see
gcloud
services.
Deploying the API backend
So far you have deployed the OpenAPI document to Service Management, but you haven't yet deployed the code that serves the API backend. This section walks you through deploying the sample API and ESP to App Engine.
To deploy the API backend:
- Add your service name to the
app.yaml
file:Java Open the
endpoints/getting-started/src/main/appengine/app.yaml
file.Python Open the
endpoints/getting-started/app.yaml
file.Go Open the
endpoints/getting-started/app.yaml
file.PHP Open the
endpoints/getting-started/app.yaml
file.Ruby Open the
endpoints/getting-started/app.yaml
file.NodeJS Open the
endpoints/getting-started/app.yaml
file.
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, up to 5 minutes 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.
app.yaml
file. Because the endpoints_api_service
section is included in the
app.yaml
file, 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.
endpoints/getting-started
directory.
This is where your openapi-appengine.yaml
configuration file is
located.Deploy using Maven:
mvn appengine:stage gcloud app deploy target/appengine-staging
gcloud app deploy
gcloud app deploy
gcloud app deploy
gcloud app deploy
gcloud app deploy
The gcloud app deploy
command creates a DNS record in the
format YOUR_PROJECT_ID.appspot.com, which you use when
you send requests to the API. 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.
Create an API key and set an environment variable
The sample code requires an API key. To simplify the request, you set an environment variable for the API key.
In the same Google Cloud project that you used for your API, create an API key on the API credentials page. If you want to create an API key in a different Google Cloud project, see Enabling an API in your Google Cloud project.
- Click Create credentials, and then select API key.
- Copy the key to the clipboard.
- Click Close.
- On your local computer, paste the API key to assign it to an environment
variable:
- In Linux or macOS:
export ENDPOINTS_KEY=AIza...
- In Windows PowerShell:
$Env:ENDPOINTS_KEY="AIza..."
- In Linux or macOS:
Send the request
Linux or Mac OS
Create an environment variable for your App Engine project URL. Replace YOUR_PROJECT_ID with your Google Cloud project ID:
export ENDPOINTS_HOST=https://YOUR_PROJECT_ID.appspot.com
Send an HTTP request using the
ENDPOINTS_HOST
andENDPOINTS_KEY
environment variables you set previously:curl --request POST \ --header "content-type:application/json" \ --data '{"message":"hello world"}' \ "${ENDPOINTS_HOST}/echo?key=${ENDPOINTS_KEY}"
In the preceding curl
:
- The
--data
option specifies the data to post to the API. - The
--header
option specifies that the data is in JSON format.
PowerShell
Create an environment variable for your App Engine project URL. Replace YOUR_PROJECT_ID with your Google Cloud project ID:
$Env:ENDPOINTS_HOST="https://YOUR_PROJECT_ID.appspot.com"
Send an HTTP request using the
ENDPOINTS_HOST
andENDPOINTS_KEY
environment variables you set previously:(Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' ` -Headers @{"content-type"="application/json"} ` -URI "$Env:ENDPOINTS_HOST/echo?key=$Env:ENDPOINTS_KEY").Content
In the previous example, the first two lines end in a backtick. When you paste the example into PowerShell, make sure there isn't a space following the backticks. For information about the options used in the example request, see Invoke-WebRequest in the Microsoft documentation.
Third-party app
You can use a third-party application such as the Chrome browser extension Postman to send the request:
- Select
POST
as the HTTP verb. - For the header, select the key
content-type
and the valueapplication/json
. - For the body, enter the following:
{"message":"hello world"}
-
In the URL, use the actual
appspot.com
address and API key rather than the environment variables. For example:
https://example-project-12345.appspot.com/echo?key=AIza...
The API echoes back the message that you send it, and responds with the following:
{
"message": "hello world"
}
If you didn't get a successful response, see Troubleshooting response errors.
You just deployed and tested an API in Endpoints!
Tracking API activity
View the activity graphs for your API in the Endpoints page.
Go to the Endpoints Services page
It might take a few moments for the request to be reflected in the graphs.
Look at the request logs for your API in the Logs Explorer page.
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 account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
See Deleting an API and API instances for information on stopping the services used by this tutorial.