Service API: notifications
- We're assuming your API key is set in the environment variable
$KEYwith the necessary permissions. - The
notification-integrationsendpoints require a key with notification-integrations scope (read). - The
notification-rulesendpoints require a key with notification-rules scope (read). - The
notification-eventsendpoint requires a key with notification-events scope (read). - Otherwise the API returns
403 Forbidden.
The Console sends notifications through three kinds of objects:
- A notification integration is a connection between your Organization and a messaging tool:
slack,webhook,discordoremail. - A notification rule decides which events trigger a notification, and sends it to one or more destinations. A destination points at an integration and holds where the message goes on that tool (a Slack or Discord channel, a webhook URL, a list of email addresses).
- A notification event type is one of the events a rule can react to, such as a Security Engine going offline.
Notification integrations and rules are created, changed and deleted from the CrowdSec Console only (see Notification integrations and Notification rule). The Service API reads them, it cannot create or change them.
Notification integrationsโ
Get Notification Integrationsโ
List the notification integrations of your Organization
The optional integration_type query parameter filters on one or more integration types
(slack, webhook, discord, email); repeat it to pass several values.
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
"https://admin.api.crowdsec.net/v1/notification-integrations?integration_type=slack&page=1&size=50"
import os
KEY = os.getenv('KEY')
from crowdsec_service_api import (
NotificationIntegrations,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = NotificationIntegrations(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_notification_integrations(
integration_type=["slack"],
page=1,
size=50,
)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
answer on success
{
"items": [
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"is_active": true,
"integration_type": "slack",
"rules_count": 2,
"token_type": "bot",
"scope": "string",
"bot_id": "string",
"bot_user_id": "string",
"team": "string",
"team_id": "string",
"enterprise_id": null,
"icon_url": "string"
}
],
"total": 0,
"page": 1,
"size": 1,
"pages": 0,
"links": {
"first": "/api/v1/users?limit=1&offset1",
"last": "/api/v1/users?limit=1&offset1",
"self": "/api/v1/users?limit=1&offset1",
"next": "/api/v1/users?limit=1&offset1",
"prev": "/api/v1/users?limit=1&offset1"
}
}
Every integration returns id, created_at, updated_at, is_active, integration_type and
rules_count (the number of rules using it). The other fields depend on integration_type:
slack:token_type,scope,bot_id,bot_user_id,team,team_id,enterprise_id,icon_url. The Slack access token is never returned.discord:guild_id,guild_name,icon_url.webhookandemail: no other field.
Get Notification Integrationโ
Retrieve a single notification integration, using the
idreturned by Get Notification Integrations
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/notification-integrations/${INTEGRATION_ID}
import os
KEY = os.getenv('KEY')
INTEGRATION_ID = os.getenv('INTEGRATION_ID')
from crowdsec_service_api import (
NotificationIntegrations,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = NotificationIntegrations(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_notification_integration(integration_id=INTEGRATION_ID)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
answer on success
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"is_active": true,
"integration_type": "discord",
"rules_count": 1,
"guild_id": "string",
"guild_name": "string",
"icon_url": "string"
}
An unknown integration_id returns 404 Not Found.
Notification rulesโ
Get Notification Rulesโ
List the notification rules of your Organization
Both query parameters are optional and can be repeated:
integration_id: only return the rules with a destination on one of these integrations.integration_type: only return the rules with a destination of one of these types (slack,webhook,discord,email).
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
"https://admin.api.crowdsec.net/v1/notification-rules?integration_id=${INTEGRATION_ID}&page=1&size=50"
import os
KEY = os.getenv('KEY')
INTEGRATION_ID = os.getenv('INTEGRATION_ID')
from crowdsec_service_api import (
NotificationRules,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = NotificationRules(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_notification_rules(
integration_id=[INTEGRATION_ID],
integration_type=None,
page=1,
size=50,
)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
answer on success
{
"items": [
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"name": "string",
"organization_id": "string",
"destinations": [
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"integration_id": "string",
"failure_state": null,
"integration_type": "slack",
"channel_id": "string",
"channel_name": "string"
}
],
"description": "string",
"events": [
{
"name": "security_engine_offline"
}
],
"event_filter": {
"all_entities": false,
"entity_ids": [],
"tag_ids": [
"string"
],
"filter_type": "stack_monitoring"
}
}
],
"total": 0,
"page": 1,
"size": 1,
"pages": 0,
"links": {
"first": "/api/v1/users?limit=1&offset1",
"last": "/api/v1/users?limit=1&offset1",
"self": "/api/v1/users?limit=1&offset1",
"next": "/api/v1/users?limit=1&offset1",
"prev": "/api/v1/users?limit=1&offset1"
}
}
events lists the event types that trigger the rule. event_filter is
optional and narrows which of those events are notified. Its filter_type is one of:
stack_monitoring:all_entities,entity_ids,tag_ids. It applies to the Security Engine, Log Processor and Remediation Component monitoring events.alert: the same fields plusall_scenariosandscenarios. It applies to thealert_triggeredandself_reportevents.
The fields of each item in destinations are described in Get Notification Rule Destinations.
Get Notification Ruleโ
Retrieve a single notification rule, using the
idreturned by Get Notification Rules
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/notification-rules/${RULE_ID}
import os
KEY = os.getenv('KEY')
RULE_ID = os.getenv('RULE_ID')
from crowdsec_service_api import (
NotificationRules,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = NotificationRules(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_notification_rule(rule_id=RULE_ID)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
answer on success
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"name": "string",
"organization_id": "string",
"destinations": [
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"integration_id": "string",
"failure_state": null,
"integration_type": "email",
"emails": [
"secops@example.com"
]
}
],
"description": "string",
"events": [
{
"name": "alert_triggered"
}
],
"event_filter": {
"all_entities": true,
"entity_ids": [],
"tag_ids": [],
"filter_type": "alert",
"all_scenarios": false,
"scenarios": [
"crowdsecurity/ssh-bf"
]
}
}
An unknown rule_id returns 404 Not Found.
Get Notification Rule Destinationsโ
List the destinations of a notification rule
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/notification-rules/${RULE_ID}/destinations
import os
KEY = os.getenv('KEY')
RULE_ID = os.getenv('RULE_ID')
from crowdsec_service_api import (
NotificationRules,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = NotificationRules(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_notification_rule_destinations(rule_id=RULE_ID)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
answer on success
{
"items": [
{
"id": "string",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z",
"integration_id": "string",
"failure_state": {
"consecutive_failures": 10,
"last_error": "string",
"last_failure_attempt_at": "2019-08-24T14:15:22Z",
"next_attempt_at": "2019-08-24T15:15:22Z"
},
"integration_type": "webhook",
"url": "https://example.com/crowdsec",
"verify_ssl": true,
"credentials": {
"credential_type": "api_key",
"header_name": "x-crowdsec-api-key"
}
}
]
}
Every destination returns id, created_at, updated_at, integration_id, integration_type and
failure_state. The other fields depend on integration_type:
slackanddiscord:channel_id,channel_name.webhook:url,verify_sslandcredentials.credentials.credential_typeisapi_key(withheader_name) orbasic_auth(withusername). The API key and password are never returned.email:emails.
failure_state is null while the destination works. It is set while deliveries to the destination
keep failing: consecutive_failures counts the failed deliveries in a row. After 10 failures in a row
the destination is suspended: notifications are skipped until next_attempt_at, and the first one
arriving after that time is attempted. next_attempt_at is null while the destination is not suspended.
An unknown rule_id returns 404 Not Found.
Notification eventsโ
Get Notification Eventsโ
List the event types a notification rule can react to
This endpoint returns the catalogue of event types, not a history of sent notifications. The
event_type values are the ones found in the events[].name field of a notification rule.
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/notification-events
import os
KEY = os.getenv('KEY')
from crowdsec_service_api import (
NotificationEvents,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = NotificationEvents(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_notification_events()
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
answer on success
{
"items": [
{
"event_type": "security_engine_offline",
"category": "stack",
"subcategory": "monitoring",
"label": "Security Engine Offline",
"description": "A Security Engine is offline (no login for 48h).",
"filter_type": "stack_monitoring"
}
]
}
category is one of alerts, stack, admin, integration or threat_hunting. filter_type
tells which event_filter of a rule applies to the event (alert or stack_monitoring), and is
null when the event cannot be filtered.