|
| 1 | +--- |
| 2 | +title: Using Copilot cloud agent via the API |
| 3 | +shortTitle: Use cloud agent via the API |
| 4 | +intro: 'You can start and manage {% data variables.copilot.copilot_cloud_agent %} tasks programmatically using the REST API.' |
| 5 | +product: '{% data reusables.gated-features.copilot-business-and-enterprise %}' |
| 6 | +versions: |
| 7 | + feature: copilot |
| 8 | +contentType: how-tos |
| 9 | +category: |
| 10 | + - Integrate Copilot with your tools |
| 11 | +--- |
| 12 | + |
| 13 | +> [!NOTE] |
| 14 | +> The agent tasks API is in {% data variables.release-phases.public_preview %} and subject to change. |
| 15 | +
|
| 16 | +You can use the agent tasks API to integrate {% data variables.copilot.copilot_cloud_agent_short %} into your own tools and workflows. For example, you can start a new task, list existing tasks, or check the status of a task. |
| 17 | + |
| 18 | +For the full API reference, see [AUTOTITLE](/rest/agent-tasks/agent-tasks). |
| 19 | + |
| 20 | +## Authentication |
| 21 | + |
| 22 | +The agent tasks API only supports user-to-server tokens. You can authenticate using a {% data variables.product.pat_generic %}, a {% data variables.product.prodname_oauth_app %} token or a {% data variables.product.prodname_github_app %} user-to-server token. |
| 23 | + |
| 24 | +Server-to-server tokens, such as {% data variables.product.prodname_github_app %} installation access tokens, are not supported. |
| 25 | + |
| 26 | +## Starting a task via the API |
| 27 | + |
| 28 | +To start a new {% data variables.copilot.copilot_cloud_agent_short %} task, send a `POST` request to `/agents/repos/{owner}/{repo}/tasks`. The only required parameter is `prompt`, which is the prompt for the agent. |
| 29 | + |
| 30 | +```shell copy |
| 31 | +curl -X POST \ |
| 32 | + -H "Accept: application/vnd.github+json" \ |
| 33 | + -H "X-GitHub-Api-Version: {{ defaultRestApiVersion }}" \ |
| 34 | + -H "Authorization: Bearer YOUR-TOKEN" \ |
| 35 | + https://api.github.com/agents/repos/OWNER/REPO/tasks \ |
| 36 | + -d '{ |
| 37 | + "prompt": "Fix the login button on the homepage", |
| 38 | + "base_ref": "main" |
| 39 | + }' |
| 40 | +``` |
| 41 | + |
| 42 | +Replace the following placeholder values: |
| 43 | + |
| 44 | +* `YOUR-TOKEN`: A {% data variables.product.pat_generic %} or {% data variables.product.prodname_github_app %} user-to-server token. |
| 45 | +* `OWNER`: The account owner of the repository. |
| 46 | +* `REPO`: The name of the repository. |
| 47 | + |
| 48 | +You can also include the following optional parameters in the request body: |
| 49 | + |
| 50 | +* `base_ref`: The base branch for the new branch and pull request. |
| 51 | +* `model`: The AI model to use for the task. If omitted, {% data variables.copilot.copilot_auto_model_selection_short %} is used. For more information about supported models, see [AUTOTITLE](/rest/agent-tasks/agent-tasks). |
| 52 | +* `create_pull_request`: A boolean that determines whether to create a pull request for the task. |
| 53 | + |
| 54 | +## Listing tasks |
| 55 | + |
| 56 | +You can list tasks for a specific repository or across all repositories you have access to. |
| 57 | + |
| 58 | +To list tasks for a specific repository: |
| 59 | + |
| 60 | +```shell copy |
| 61 | +curl -H "Accept: application/vnd.github+json" \ |
| 62 | + -H "X-GitHub-Api-Version: {{ defaultRestApiVersion }}" \ |
| 63 | + -H "Authorization: Bearer YOUR-TOKEN" \ |
| 64 | + https://api.github.com/agents/repos/OWNER/REPO/tasks |
| 65 | +``` |
| 66 | + |
| 67 | +To list your tasks across all repositories: |
| 68 | + |
| 69 | +```shell copy |
| 70 | +curl -H "Accept: application/vnd.github+json" \ |
| 71 | + -H "X-GitHub-Api-Version: {{ defaultRestApiVersion }}" \ |
| 72 | + -H "Authorization: Bearer YOUR-TOKEN" \ |
| 73 | + https://api.github.com/agents/tasks |
| 74 | +``` |
| 75 | + |
| 76 | +## Checking the status of a task |
| 77 | + |
| 78 | +To check the status of a specific task, send a `GET` request with the task ID: |
| 79 | + |
| 80 | +```shell copy |
| 81 | +curl -H "Accept: application/vnd.github+json" \ |
| 82 | + -H "X-GitHub-Api-Version: {{ defaultRestApiVersion }}" \ |
| 83 | + -H "Authorization: Bearer YOUR-TOKEN" \ |
| 84 | + https://api.github.com/agents/repos/OWNER/REPO/tasks/TASK-ID |
| 85 | +``` |
| 86 | + |
| 87 | +Replace `TASK-ID` with the ID of the task you want to check. You can get this ID from the response when you create a task or list tasks. The response includes the task's current `state`, which can be one of: `queued`, `in_progress`, `completed`, `failed`, `idle`, `waiting_for_user`, `timed_out`, or `cancelled`. |
| 88 | + |
| 89 | +## Further reading |
| 90 | + |
| 91 | +* [AUTOTITLE](/rest/agent-tasks/agent-tasks) |
| 92 | +* [AUTOTITLE](/copilot/concepts/agents/cloud-agent/about-cloud-agent) |
| 93 | +* [AUTOTITLE](/copilot/how-tos/use-copilot-agents/cloud-agent/start-copilot-sessions) |
0 commit comments