# create()

> Agent Tasks are currently in beta. If you run into any issues, please reach out to our [support team](https://clerk.com/support).

Creates an [`AgentTask`](https://clerk.com/docs/reference/types/agent-task.md) that generates a URL which, when visited, creates a session for the specified user. This is useful for automated testing or agent-driven flows where full authentication isn't practical.

```ts
function create(params: CreateAgentTaskParams): Promise<AgentTask>
```

## `CreateAgentTaskParams`

| Name                         | Type                                           | Description                                                                                                                          |
| ---------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| onBehalfOf                   | { userId: string } | { identifier: string } | The user to create the Agent Task for. Provide either a userId or an identifier (e.g., an email address, phone number, or username). |
| permissions                  | string                                         | The permissions the Agent Task will have. Currently, '\*' is the only supported value, which grants all permissions.                 |
| agentName                    | string                                         | The name of the agent creating the task.                                                                                             |
| taskDescription              | string                                         | A description of the Agent Task.                                                                                                     |
| redirectUrl                  | string                                         | The URL to redirect to after the Agent Task is consumed.                                                                             |
| sessionMaxDurationInSeconds? | number                                         | The maximum duration in seconds for the session created by the Agent Task. Defaults to 30 minutes (1800 seconds).                    |

## Usage

> Using `clerkClient` varies based on the SDK you're using. Refer to the [overview](https://clerk.com/docs/reference/backend/overview.md) for usage details, including guidance on [how to access the `userId` and other properties](https://clerk.com/docs/reference/backend/overview.md#example-get-the-user-id-and-other-properties).

```tsx
const agentTask = await clerkClient.agentTasks.create({
  onBehalfOf: {
    userId: 'user_123',
  },
  permissions: '*',
  agentName: 'my-agent',
  taskDescription: 'Perform automated action',
  redirectUrl: 'https://example.com/dashboard',
})

// agentTask.url is the URL to visit to authenticate the user
```

## Example

filename: app/api/example/route.ts
```ts
import { auth, clerkClient } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'

export async function POST() {
  // Use the `auth()` helper to access the `isAuthenticated` and the user's ID
  const { isAuthenticated, userId } = await auth()

  // Protect the route from unauthenticated users
  if (!isAuthenticated) {
    return new NextResponse('Unauthorized', { status: 401 })
  }

  // Instantiate the `clerkClient`
  const client = await clerkClient()

  // Use the `createAgentTask()` method to create the Agent Task
  const agentTask = await client.agentTasks.create({
    onBehalfOf: {
      userId,
    },
    permissions: '*',
    agentName: 'my-agent',
    taskDescription: 'Automated test login',
    redirectUrl: 'http://localhost:3000/dashboard',
  })

  return NextResponse.json({ message: 'Agent Task created', agentTask })
}
```

## Backend API (BAPI) endpoint

This method in the SDK is a wrapper around the BAPI endpoint `POST/agents/tasks`. See the [BAPI reference](https://clerk.com/docs/reference/backend-api/tag/agent-tasks/POST/agents/tasks){{ target: '_blank' }} for more information.

---

## Sitemap

[Overview of all docs pages](https://clerk.com/docs/llms.txt)
