Deploying and Testing an API

This page describes how to deploy your API.

Before you begin

This page assumes that you have:

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:[Path-to-Google-Cloud-SDK]/platform/google_appengine.

    Replace [Path-to-Google-Cloud-SDK] with the output of the following command:

     gcloud info --format="value(installation.sdk_root)"
    
  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 --request POST \
        --header "Content-Type: application/json" \
        --data '{"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"
     }
    

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

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 generate and deploy the OpenAPI document:

  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_openapi_spec main.EchoApi \
         --hostname [YOUR_PROJECT_ID].appspot.com
    

    Replace [YOUR_PROJECT_ID] with your GCP project ID. Ignore the warnings that are displayed.

    You can list multiple classes on the command line. Endpoints will generate an OpenAPI document for each API name/version combination. If you want to deploy multiple API classes with different API names as part of a single service, you must also add the --x-google-api-name flag. Enabling this flag adds extra restrictions on your API names. Specifically, the names must match the regular expression [a-z][a-z0-9]{0,39}; that is, the name must consist of 1-40 characters, which may all be either lowercase alphabetical characters or numbers, except that the first character may not be a number. For example:

     python lib/endpoints/endpointscfg.py get_openapi_spec main.FooApi main.BarApi \
         --hostname your-service.appspot.com \
         --x-google-api-name
    
  3. Deploy the configuration file, for example:

     gcloud endpoints services deploy echov1openapi.json
    

    If you have multiple OpenAPI documents, list them all on the command line. For example:

     gcloud endpoints services deploy foov1openapi.json barv1openapi.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 --request POST \
        --header "Content-Type: application/json" \
        --data '{"content":"echo"}' \
        "${HOST}/_ah/api/echo/v1/echo?n=2"
    
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Endpoints Frameworks for App Engine
Need help? Visit our support page.