Service API: tags
- We're assuming your API key is set in the environment variable
$KEYwith the necessary permissions. - The
tagsendpoints require a key with tags scope;GETneeds read access,POST/PATCH/DELETEneed write access, otherwise the API returns403 Forbidden.
A tag is a label owned by your Organization that groups Security Engines and integrations, the same tags you see in the Console. Once linked, a tag can be used to filter engines, or as a target when you subscribe a blocklist or create a decision.
Tag names are stored in lowercase and are unique within your Organization. In the paths below, ${TAG_ID} is the
tag name (for example production).
Create Tagโ
Create a tag for your Organization
curl -i -H "x-api-key: ${KEY}" -X POST -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/tags \
-d '{ "name": "production", "description": "Production servers" }'
import os
KEY = os.getenv('KEY')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from crowdsec_service_api.models import TagCreateRequest
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
request = TagCreateRequest(name="production", description="Production servers")
try:
response = client.create_tag(request=request)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
If a tag with the same name already exists, the API returns 409 Conflict.
answer on success (201 Created)
{
"id": "string",
"name": "string",
"description": "string",
"created_at": "2019-08-24T14:15:22Z",
"organization_id": "string"
}
Get Tagsโ
List the tags of your Organization
Optional query parameters:
name_starts_with: only return tags whose name starts with this value (e.g.prod)page: page number, default1size: page size, default50
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
"https://admin.api.crowdsec.net/v1/tags?page=1&size=50"
import os
KEY = os.getenv('KEY')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_tags(
name_starts_with=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",
"name": "string",
"description": "string",
"created_at": "2019-08-24T14:15:22Z",
"organization_id": "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"
}
}
Get Tagโ
Retrieve a single tag by its name
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/tags/${TAG_ID}
import os
KEY = os.getenv('KEY')
TAG_ID = os.getenv('TAG_ID')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
try:
response = client.get_tag(tag_id=TAG_ID)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
If the tag does not exist, the API returns 404 Not Found.
answer on success
{
"id": "string",
"name": "string",
"description": "string",
"created_at": "2019-08-24T14:15:22Z",
"organization_id": "string"
}
Update Tagโ
Update the description of a tag. The name of a tag cannot be changed.
curl -i -H "x-api-key: ${KEY}" -X PATCH -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/tags/${TAG_ID} \
-d '{ "description": "Production servers, EU region" }'
import os
KEY = os.getenv('KEY')
TAG_ID = os.getenv('TAG_ID')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from crowdsec_service_api.models import TagPatchRequest
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
request = TagPatchRequest(description="Production servers, EU region")
try:
response = client.update_tag(tag_id=TAG_ID, request=request)
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",
"name": "string",
"description": "string",
"created_at": "2019-08-24T14:15:22Z",
"organization_id": "string"
}
Link Entities To Tagโ
Link one or more Security Engines or integrations to a tag
entities takes the ids returned by Get Engines or by the
integrations endpoints. The tag must already exist.
curl -i -H "x-api-key: ${KEY}" -X POST -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/tags/${TAG_ID}/entities \
-d '{ "entities": ["'"${ENGINE_ID}"'", "'"${INTEGRATION_ID}"'"] }'
import os
KEY = os.getenv('KEY')
TAG_ID = os.getenv('TAG_ID')
ENGINE_ID = os.getenv('ENGINE_ID')
INTEGRATION_ID = os.getenv('INTEGRATION_ID')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from crowdsec_service_api.models import TagLinkEntityRequest
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
request = TagLinkEntityRequest(entities=[ENGINE_ID, INTEGRATION_ID])
try:
response = client.link_tag(tag_id=TAG_ID, request=request)
print(response.model_dump_json())
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
Each id is checked on its own: modified_count counts the entities that were linked, and errors lists the ids
that could not be linked (unknown id, or an entity that is neither an engine nor an integration).
answer on success (201 Created)
{
"modified_count": 1,
"errors": {
"string": "Entity not found or not of supported type"
}
}
Unlink Entity From Tagโ
Remove a tag from a Security Engine or an integration. The tag itself is not deleted.
curl -i -H "x-api-key: ${KEY}" -X DELETE -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/tags/${TAG_ID}/entities/${ENGINE_ID}
import os
KEY = os.getenv('KEY')
TAG_ID = os.getenv('TAG_ID')
ENGINE_ID = os.getenv('ENGINE_ID')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
try:
client.unlink_tag(tag_id=TAG_ID, entity_id=ENGINE_ID)
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
On success the API returns 204 No Content with an empty body.
Delete Tagโ
Delete a tag of your Organization. The tag is first removed from every engine and integration it is linked to.
curl -i -H "x-api-key: ${KEY}" -X DELETE -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/tags/${TAG_ID}
import os
KEY = os.getenv('KEY')
TAG_ID = os.getenv('TAG_ID')
from crowdsec_service_api import (
Tags,
Server,
ApiKeyAuth,
)
from httpx import HTTPStatusError
auth = ApiKeyAuth(api_key=KEY)
client = Tags(base_url=Server.production_server.value, auth=auth)
try:
client.delete_tag(tag_id=TAG_ID)
except HTTPStatusError as e:
print(f"An error occurred: {e.response.status_code} - {e.response.text}")
On success the API returns 204 No Content with an empty body.