Join the Actions on Google Developer Challenge to win a trip to Google I/O 2018 and more than 20 other prizes.

Deploy Fulfillment

We'll show you how to set up your environment to locally test and develop with the Google Cloud Functions local emulator and to deploy to Google Cloud Functions for production hosting.

Set up local fulfillment

When developing, it's very useful to have a local fulfillment server so you can quickly deploy code changes and test them. However, because your app is deployed to the web, you must use a tool like ngrok to expose the fulfillment server to the internet. Here's our recommended environment for developing fulfillment locally:

  • Cloud Functions local emulator - The emulator lets you deploy a Cloud Function locally for quick testing.

  • ngrok - This tool creates an externally addressable URL (API.AI requires this) for a local development server. This lets you test your fulfillment logic with API.AI without having to deploy to a production Google Cloud Function.

  • Node.js - Node.js is our preferred development language, and our samples and client library are Node.js based. You'll also install the Google Cloud Functions local emulator with the Node Package Manager (npm).

To set up your environment:

  1. Download and install Node.js.
  2. Install the Google Cloud local emulator:

    npm install -g @google-cloud/functions-emulator
    
  3. Download and install ngrok. Ngrok is a single file binary, so wherever you unpack it to, it's convenient to add that directory to your system path.

  4. Install the sample dependencies for the Node.js fulfillment, where <cloud_function_dir> is the directory that contains your fulfillment (for example, the directory that contains your index.js and package.json files).

    cd <cloud_function_dir>
    npm install
    
  5. Run the following commands to deploy the function.

    cd <cloud_function_dir>
    functions start
    functions deploy cloudFunctionName --trigger-http
    

    The output of the commands displays a table of information. Make note of the URL specified by the Resource row. You'll need the port and path of that URL. For example, if the resource row shows:

    http://localhost:8010/myAppProject/us-central1/myCloudFunction
    

    the port is 8010 and the path is /myAppProject/us-central1/myCloudFunction.

  6. Run ngrok with the port specified by the Resource URL:

    <ngrok-dir>/ngrok http <port>
    

    Your fulfillment URL is the ngrok hostname appended with the path of the Resource URL. For example:

    https://d4b9db8d.ngrok.io/myAppProject/us-central1/myCloudFunction
    
  7. In API.AI's left navigation, click Fulfillment and enter the fulfillment URL in the URL field.

Set up production fulfillment

  1. Download and install Node.js.
  2. Set up and initialize the Firebase CLI. If the following command fails with an EACCES error, you may need to change npm permissions.

    npm install -g firebase-tools
    
  3. Authenticate the firebase tool with your Google account:

    firebase login
    
  4. Associate the firebase tool with your Actions project:

    firebase use PROJECT_ID
    
  5. Get the fulfillment dependencies and deploy the fulfillment:

        cd <<cloud_function_dir>>/functions
        npm install
        firebase deploy --only functions
    

    The deployment takes a few minites. Once completed, you'll see output similar to the following. You'll need the Function URL to enter in API.AI.

    ✔  Deploy complete!
    Project Console: https://console.firebase.google.com/project/factsapp-abb1d/overview Function URL (factsAboutGoogle): https://us-central1-factsapp-abb1d.cloudfunctions.net/factsAboutGoogle

  6. In API.AI's left navigation, click Fulfillment, move the ENABLED slider to the right, and enter the Function URL in the URL field. For example, if your project ID is factsapp-abb1d, your API.AI fulfillment setting might look like this screenshot.