> ## Documentation Index
> Fetch the complete documentation index at: https://docs.molesignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 新建告警来源

> 新建告警来源，拿到它的 Token。

```http theme={null}
POST /api/v1/alerts/sources
```

新建一个[告警来源](https://docs.molesignal.com/zh-Hans/alerting/sources.md)，需要 `alerts.sources.manage`。响应里的 `token` 只出现这一次，用 Token 调用[接入外部告警](https://docs.molesignal.com/zh-Hans/api/alerting/send-alerts.md)。开了联邦时，来源会同步到其他集群。

| 字段 | 类型 | 说明 |
| - | - | - |
| `kind` | string | 新建时必填：`api`、`alertmanager`、`grafana`、`datadog`、`pagerduty`、`aliyun_cms`、`tencent_monitor`、`aws_cloudwatch`、`zabbix` 或 `email`。 |
| `name` | string | 必填，最多 128 个字符。 |
| `enabled` | boolean | 默认 `true`。 |
| `escalation_policy_id` | string \| null | 本组织的升级策略 ID，或 `default`。不带或为 `null` 时不升级，只按通知策略通知。 |
| `severity` | string | 告警没带严重度时用的：`critical`、`error`、`warning`（默认）或 `info`。 |
| `labels` | object | 附加标签，最多 32 个；键由字母、数字和 `_ . - : /` 组成，不能以 `alert.` 开头，也不能是 `external.kind`。 |
| `email` | object | 邮件来源怎么读一封邮件：`allowed_senders`（完整地址、`@域名` 或 `*`）、`resolve_pattern`、`dedup_pattern`、`ignore_pattern`，见[邮件](https://docs.molesignal.com/zh-Hans/alerting/sources.md#邮件)。修改时不带保持不变。 |
| `home_cluster` | string | 开事件的联邦集群，须是本集群或启用的远端集群。不带时新建的来源用本集群；没开联邦时不起作用。 |

```bash cURL theme={null}
curl -X POST http://localhost:5080/api/v1/alerts/sources \
  -H "authorization: Bearer $MS_TOKEN" \
  -H "content-type: application/json" \
  -d '{"kind": "alertmanager", "name": "Production Prometheus", "escalation_policy_id": "default", "labels": {"team": "payments"}}'
```

响应是来源加上 `token`，其余字段见[列出告警来源](https://docs.molesignal.com/zh-Hans/api/alerting/list-sources.md)。

```json 200 theme={null}
{
  "id": "3KEJ1mZqf0bY0b8XUgD4tkd2Vwq",
  "name": "Production Prometheus",
  "kind": "alertmanager",
  "enabled": true,
  "escalation_policy_id": "default",
  "severity": "warning",
  "labels": { "team": "payments" },
  "token_prefix": "ms_aB3kZ1xT9pQrU7nM",
  "home_cluster": "",
  "intake_path": "/api/v1/intake/alerts",
  "created_by": "<user-id>",
  "created_at": 1790000000000000,
  "updated_at": 1790000000000000,
  "last_intake_at": null,
  "token": "<token>"
}
```

**错误**

| 状态码 | 原因 |
| - | - |
| `400` | 字段不对，例如名称为空、升级策略不存在、接收集群不是本集群也不是启用的远端集群。 |
| `403` | Token 缺少 `alerts.sources.manage`。 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.