Blockkit

2.1.3 · active · verified Thu Apr 16

Blockkit is a Python library for building user interfaces for Slack apps. It provides a fluent API with type hints and validation, allowing developers to compose Slack's Block Kit elements using Python classes, which reduces boilerplate and improves maintainability. The library is currently at version 2.1.3 and is actively maintained with frequent releases.

Common errors

Warnings

Install

Imports

Quickstart

This example constructs a Slack message with an approval request, including a button with a confirmation dialog. The `.build()` method validates the Block Kit structure, ensuring compliance with Slack's API specifications. The generated `message` object can be directly passed to the `blocks` parameter of `slack_sdk.WebClient.chat_postMessage`.

from blockkit import Message, Section, Button, Confirm

message = (
    Message()
    .add_block(Section("Please approve *Alice's* expense report for $42")) # Markdown detected automatically
    .accessory(
        Button("Approve")
        .action_id("approve_button")
        .style(Button.PRIMARY)
        .confirm(
            Confirm()
            .title("Are you sure?")
            .text("This action cannot be undone")
            .confirm("Yes, approve")
            .deny("Cancel")
        )
    )
    .thread_ts(os.environ.get('SLACK_THREAD_TS', '1234567890')) # Converts types automatically
    .build() # Validates everything: types, lengths, required fields
)

# To send this message via Slack's WebClient, you would typically do:
# from slack_sdk import WebClient
# client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
# response = client.chat_postMessage(
#     channel="C12345",
#     blocks=message # Blockkit objects are directly serializable to JSON dicts expected by Slack SDK
# )

print(message)

view raw JSON →