Deploying and Testing an API

This page describes how to deploy your API.

Before you begin

Deploying your API to production

To deploy an API:

  1. If this is the first time you are deploying the API, or if you have modified your API decorators, generate and deploy your API configuration file before deploying your API.

  2. Set the ENDPOINTS_GAE_SDK environment variable to the path of your App Engine SDK folder, for example [Path-to-Google-Cloud-SDK]/platform/google_appengine.

  3. Invoke the following command in the API main directory:

     gcloud app deploy
    
  4. Wait a few moments for the deployment to succeed, ignoring the warning messages. When the deployment completes, you'll see a message similar to the following:

     File upload done.
     Updating service [default]...done.
    
  5. Confirm successful deployment by sending a test request to the API, for example:

     curl -H "Content-Type: application/json" -X POST -d '{"content":"echo"}' https://[PROJECT-ID].appspot.com/_ah/api/echo/v1/echo?n=2
    

    Replace [PROJECT-ID] with your project ID and echo with your API name.

  6. View the results: you should see:

     {
      "content": "echo echo"
     }
    

Generating and deploying an API configuration file

You must regenerate and re-deploy your API configuration when you deploy your API for the first time, and generally whenever you change configuration details in your API decorators.

To deploy the API configuration file:

  1. Make sure you have added API management to your project.

  2. From the API’s main directory, generate an OpenAPI document using the framework tools, for example:

     python lib/endpoints/endpointscfg.py get_swagger_spec main.EchoApi --hostname your-service.appspot.com
    

    Replace your-service with your project ID. Ignore the warnings that are displayed.

  3. Deploy the configuration file, for example:

     gcloud endpoints services deploy echov1openapi.json
    

    The command returns several lines of information, including a line similar to the following:

     Service Configuration with version "[2016-12-01R0]" uploaded for service "[YOUR-PROJECT-ID].appspot.com"
    
  4. Note the service configuration version shown in the returned lines, for example in the above lines, 2016-12-01R0. you need this later in this procedure.

    You can always find the version later by invoking the command

     gcloud endpoints configs list --service=[YOUR-PROJECT-ID].appspot.com
    

    replacing [PROJECT-ID] with your project ID.

  5. Edit your project's file app.yaml env_variables section as follows:

     env_variables:
       # Replace with your endpoints service name.
       ENDPOINTS_SERVICE_NAME: [YOUR-PROJECT-ID].appspot.com
       # Replace with the version Id of your uploaded Endpoints service.
       ENDPOINTS_SERVICE_VERSION: [YOUR-SERVICE-VERSION]
    

    Replace [YOUR-PROJECT-ID] with your project ID, and [YOUR-SERVICE-VERSION] with your service configuration version from the previous step. (Do not include the square brackets.)

  6. Re-deploy your API:

     gcloud app deploy
    

Deploying to multiple app versions

For information on how to do API versioning, and how to deploy to versions, see Handling API Versioning

Testing an API locally

To run your API locally:

  1. From the root directory of the project, start the App Engine development server:

    [PATH-TO-SDK]dev_appserver.py [YOUR-PROJECT-DIR]
    

    Replace [PATH-TO-SDK] with the path where you installed the App Engine SDK for Python. Replace [YOUR-PROJECT-DIR] with the name of the main directory for your API, including path.

  2. Invoke curl to make an API request:

     curl \
         -H "Content-Type: application/json" \
         -X POST \
         -d '{"message": "echo"}' \
         "${HOST}/_ah/api/echo/v1/echo?n=2"
    

Send feedback about...

Cloud Endpoints Frameworks for App Engine