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

> [!NOTE]
> **Note:** You must have a [role with full or edit access](https://docs.cloud.google.com/dialogflow/es/docs/access-control.md.txt#roles) in order to create or delete an agent.

## Create an agent

The following examples show how to call the `SetAgent` method for the
[Agent](https://docs.cloud.google.com/dialogflow/es/docs/reference/common-types.md.txt#agents)
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:

- <var translate="no">PROJECT_ID</var>: 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:

#### curl (Linux, macOS, or Cloud Shell)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`,
and execute the following command:

```
curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "x-goog-user-project: PROJECT_ID" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d @request.json \
     "https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent"
```

#### PowerShell (Windows)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`,
and execute the following command:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent" | Select-Object -Expand Content
```

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 CX, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```java

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 CX, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```javascript
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 CX, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```python
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)

```

<br />

## Delete an agent

The following examples show how to call the `DeleteAgent` method for the
[Agent](https://docs.cloud.google.com/dialogflow/es/docs/reference/common-types.md.txt#agents)
type.

### REST


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

- <var translate="no">PROJECT_ID</var>: 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:

#### curl (Linux, macOS, or Cloud Shell)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Execute the following command:

```
curl -X DELETE \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "x-goog-user-project: PROJECT_ID" \
     "https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent"
```

#### PowerShell (Windows)

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Execute the following command:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }

Invoke-WebRequest `
    -Method DELETE `
    -Headers $headers `
    -Uri "https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent" | Select-Object -Expand Content
```

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

<br />