Manage agents with the API

Normally, you create and delete agents using the console. However, in certain advanced scenarios, you may find it easier to use the API.

Create an agent

The following examples show how to call the SetAgent method for the Agent type. These examples are creating agents, but the same methods can be used to update agent settings, like agent edition.

REST

Before using any of the request data, make the following replacements:

  • PROJECT_ID: your Google Cloud project ID

HTTP method and URL:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent

Request JSON body:

{
  "displayName": "My-display-name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "parent": "projects/PROJECT_ID",
  "displayName": "My display name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York",
  "apiVersion": "API_VERSION_V2"
}

Java

To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import com.google.cloud.dialogflow.v2.Agent;
import com.google.cloud.dialogflow.v2.Agent.Builder;
import com.google.cloud.dialogflow.v2.AgentsClient;
import com.google.cloud.dialogflow.v2.AgentsSettings;
import java.io.IOException;

public class SetAgent {

  public static void main(String[] args) throws IOException {
    String projectId = "my-project-id";

    // The display name will set the name of your agent
    String displayName = "my-display-name";

    setAgent(projectId, displayName);
  }

  public static Agent setAgent(String parent, String displayName) throws IOException {

    AgentsSettings agentsSettings = AgentsSettings.newBuilder().build();
    try (AgentsClient client = AgentsClient.create(agentsSettings)) {
      // Set the details of the Agent to create
      Builder build = Agent.newBuilder();

      build.setDefaultLanguageCode("en");
      build.setDisplayName(displayName);

      Agent agent = build.build();

      // Make API request to create agent
      Agent response = client.setAgent(agent);
      System.out.println(response);
      return response;
    }
  }
}

Node.js

To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

const {AgentsClient} = require('@google-cloud/dialogflow');

// make sure to pass projectID as the input parameter
const parent = 'projects/' + parentId + '/locations/global';

const agent = {
  parent: parent,
  displayName: displayName,
  defaultLanguageCode: 'en',
  timeZone: 'America/Los_Angeles',
};

const client = new AgentsClient();

async function setAgent() {
  const request = {
    agent,
  };

  const [response] = await client.setAgent(request);
  console.log(`response: ${JSON.stringify(response, null, 2)}`);
}
await setAgent();

Python

To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from google.cloud.dialogflow_v2 import Agent, AgentsClient, SetAgentRequest
import google.protobuf.field_mask_pb2


def set_agent(project_id, display_name):
    agents_client = AgentsClient()

    parent = agents_client.common_project_path(project_id)

    agent = Agent(
        parent=parent,
        display_name=display_name,
        default_language_code="en",
        time_zone="America/Los_Angeles",
    )

    update_mask = google.protobuf.field_mask_pb2.FieldMask()
    update_mask.FromJsonString("displayName,defaultLanguageCode,timeZone")

    request = SetAgentRequest(
        agent=agent,
        update_mask=update_mask,
    )

    return agents_client.set_agent(request=request)

Delete an agent

The following examples show how to call the DeleteAgent method for the Agent type.

REST

Before using any of the request data, make the following replacements:

  • PROJECT_ID: your Google Cloud project ID

HTTP method and URL:

DELETE https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent

To send your request, expand one of these options:

You should receive a successful status code (2xx) and an empty response.