Slack Bot / Webhook Token Regex for Python
/^(xoxb|xoxp|xoxa|xoxr|xoxs|xapp)-[A-Za-z0-9\-]{10,200}$|^https://hooks\.slack\.com/services/T[A-Z0-9]+/B[A-Z0-9]+/[A-Za-z0-9]+$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching slack bot / webhook token, ported and verified for Python. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# Slack Bot / Webhook Token
# ReDoS-safe | RegexVault — Security > API Keys & Tokens
import re
slack_bot_webhook_token_pattern = re.compile(r'^(xoxb|xoxp|xoxa|xoxr|xoxs|xapp)-[A-Za-z0-9\-]{10,200}$|^https://hooks\.slack\.com/services/T[A-Z0-9]+/B[A-Z0-9]+/[A-Za-z0-9]+$')
def validate_slack_bot_webhook_token(value: str) -> bool:
return bool(slack_bot_webhook_token_pattern.fullmatch(value))
# Example
print(validate_slack_bot_webhook_token("xoxb-123456789012-123456789012-ABC123def456GHI789jkl0")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
xoxb-123456789012-123456789012-ABC123def456GHI789jkl0 | xox-invalid-format |
xoxp-1234-5678-9012-abcdef1234567890abcdef | slack_token_aBcDeFgHiJkLmNoPqRsTuV |
https://hooks.slack.com/services/T12345/B12345/AbCdEfGhIj | xoxb-short |
When to use this pattern
This pattern is drawn from the Security > API Keys & Tokens category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Bot tokens (xoxb) can access all channels the bot is in. User tokens (xoxp) act as that specific user. A leaked user token is equivalent to that user's password for Slack.
Technical Notes
Token prefixes: xoxb=bot token, xoxp=user token, xoxa=app-level token (legacy), xoxr=refresh token, xoxs=workspace token (legacy), xapp=Socket Mode app token. Webhook URLs are for incoming webhooks only and cannot read messages.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern