# Link an agent to an analysis group

> POST https://api.nabrah.ai/api/ext/analysis/{analysis_group_id}/agent

Group: Analysis groups. Page: https://www.nabrah.ai/en/docs/api-reference/analysis-groups/link-an-agent-to-an-analysis-group

Links a single agent to the analysis group identified by `analysis_group_id` and returns the new `link_id`. This is the analysis-side counterpart to the agent linking endpoints and is convenient when you want to attach one specific agent. The agent must already exist (create it with POST /api/ext/agent). Save the returned `link_id` to unlink later via DELETE /api/ext/analysis/link/{link_id}.

## Where the IDs come from

- **Path and body IDs**: `analysis_group_id` (path) comes from GET /api/ext/analysis. `agent_id` (body) comes from GET /api/ext/agent.

## Response shape

- **link_id**: Returns `{ "link_id": "..." }`. Use this to remove the link with DELETE /api/ext/analysis/link/{link_id}. To link many agents at once, use POST /api/ext/analysis/{analysis_group_id}/agents instead. Fields: `link_id`.

## Authorization

- `X-API-Key` (string, header, required): your workspace API key.

## Path parameters

- `analysis_group_id` (string, required): UUID of the analysis group to link an agent to. Get this from GET /api/ext/analysis or from the create response (POST /api/ext/analysis).

## Body (application/json)

### Example: Link agent

Link one agent to this analysis group.

```json
{
  "agent_id": "018f3a2b-9c4d-7e1a-bf52-0a1b2c3d4e5f"
}
```

## Response

### 200 Successful Response

Content type: `application/json`

```json
{
  "link_id": "00000000-0000-4000-8000-000000000000"
}
```

### 422 Validation Error

Content type: `application/json`

```json
{
  "detail": [
    {
      "loc": [
        "string"
      ],
      "msg": "string",
      "type": "string"
    }
  ]
}
```

## Request examples

### cURL

```bash
curl --request POST \
  --url 'https://api.nabrah.ai/api/ext/analysis/<analysis_group_id>/agent' \
  --header 'X-API-Key: <your-api-key>' \
  --header 'Content-Type: application/json' \
  --data '{
  "agent_id": "018f3a2b-9c4d-7e1a-bf52-0a1b2c3d4e5f"
}'
```

### Python

```python
import requests

url = "https://api.nabrah.ai/api/ext/analysis/<analysis_group_id>/agent"
headers = {
    "X-API-Key": "<your-api-key>",
}
payload = {
    "agent_id": "018f3a2b-9c4d-7e1a-bf52-0a1b2c3d4e5f"
}

response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
print(response.json())
```

### JavaScript

```javascript
const response = await fetch("https://api.nabrah.ai/api/ext/analysis/<analysis_group_id>/agent", {
  method: "POST",
  headers: {
    "X-API-Key": "<your-api-key>",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "agent_id": "018f3a2b-9c4d-7e1a-bf52-0a1b2c3d4e5f"
  }),
});

const data = await response.json();
console.log(data);
```

### Go

```go
package main

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

func main() {
	payload := strings.NewReader(`{
  "agent_id": "018f3a2b-9c4d-7e1a-bf52-0a1b2c3d4e5f"
}`)

	req, _ := http.NewRequest("POST", "https://api.nabrah.ai/api/ext/analysis/<analysis_group_id>/agent", payload)
	req.Header.Set("X-API-Key", "<your-api-key>")
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	out, _ := io.ReadAll(res.Body)
	fmt.Println(string(out))
}
```
