Quick Start
Install slackblocks and build your first valid Block Kit message. The language selector in the top navigation controls the instructions and examples on this page.
Install the package
Python 3.10 or newer is required.
pip install slackblocks
Build a message
from slackblocks import Message, SectionBlock
message = Message(
channel="#general",
text="Hello from slackblocks!",
blocks=[SectionBlock("Hello, world!")],
)
print(message.json())
Message validates the payload as it is constructed and renders it as Slack-compatible JSON. The text value is the plain-text fallback Slack uses for notifications and accessibility.
Install the package
Node.js 20.19 or newer is required. The package is ESM-only.
pnpm add @nicklambourne/slackblocks
Build a message
import { Message, SectionBlock } from "@nicklambourne/slackblocks";
const payload = Message()
.channel("C0123456")
.text("Hello from slackblocks!")
.blocks(SectionBlock().text("Hello, world!"))
.build();
console.log(JSON.stringify(payload, null, 2));
Each builder uses typed camelCase setters. .build() validates the completed
payload and returns the plain snake_case object expected by Slack.
Install the module
Go 1.22 or newer is required.
mkdir slackblocks-hello
cd slackblocks-hello
go mod init example.com/slackblocks-hello
go get github.com/nicklambourne/slackblocks/go/v2
Build a message
package main
import (
"context"
"os"
slackblocks "github.com/nicklambourne/slackblocks/go/v2"
slack "github.com/slack-go/slack"
)
func main() {
client := slack.New(os.Getenv("SLACK_API_TOKEN"))
_, _, err := client.PostMessageContext(
context.Background(),
os.Getenv("SLACK_CHANNEL_ID"),
slack.MsgOptionText("Hello from slackblocks!", false),
slack.MsgOptionBlocks(
slackblocks.NewSectionBlock().Text("Hello, world!"),
),
)
if err != nil {
panic(err)
}
}
The block builder implements slack.Block and validates when slack-go marshals it, before the request is sent. The channel and fallback text stay in slack-go, where its other message options can be added normally. Use NewMessage().Build() instead when you need a complete JSON-compatible Object for another client.
Install the artifact
Java 17 or newer is required. Add slackblocks and the official Slack Java SDK client to your Maven project:
<dependency>
<groupId>io.github.nicklambourne</groupId>
<artifactId>slackblocks</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>slack-api-client</artifactId>
<version>1.51.0</version>
</dependency>
Build and send a message
import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import io.github.nicklambourne.slackblocks.block.SectionBlock;
import java.util.List;
MethodsClient client = Slack.getInstance().methods(System.getenv("SLACK_API_TOKEN"));
SectionBlock block = SectionBlock.builder().markdownText("Hello, world!").build();
client.chatPostMessage(ChatPostMessageRequest.builder()
.channel("C0123456")
.text("Hello from slackblocks!")
.blocks(List.of(block))
.build());
build() validates the completed value and returns an immutable object. Blocks implement the Slack SDK's LayoutBlock directly, so the channel, fallback text, threading, and other delivery options remain on the SDK request where Java developers expect them.
Install the package
.NET 8 or newer is required. Add slackblocks to your project:
dotnet add package Slackblocks
Build and send a message
using System.Net.Http.Headers;
using System.Text;
using Slackblocks.Blocks;
using Slackblocks.Payloads;
var message = new MessagePayload(
"C0123456",
text: "Hello from slackblocks!",
blocks: [new SectionBlock(text: "Hello, world!")]);
using var http = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Post, "https://slack.com/api/chat.postMessage")
{
Content = new StringContent(message.ToJson(), Encoding.UTF8, "application/json"),
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("SLACK_BOT_TOKEN"));
using var response = await http.SendAsync(request);
response.EnsureSuccessStatusCode();
Each constructor validates its arguments and returns an immutable value. MessagePayload also checks the message as a whole, including the 50-block limit and which blocks messages accept, before any request is made. Slack reports Web API errors in the response body's ok and error fields, so check those as well as the HTTP status.
Install the gem
MRI Ruby 3.3 or newer is required:
gem install slackblocks
Build a message
require "slackblocks"
message = Slackblocks::MessagePayload.new(
channel: "C01234567",
text: "Hello from slackblocks!",
blocks: [Slackblocks::SectionBlock.new(text: "Hello, world!")]
)
puts message.to_json
text is the plain-text fallback for notifications and accessibility. Values are validated when constructed, and to_json emits Slack-compatible JSON.
Install the crate
Rust 1.85 or newer is required; the crate uses edition 2024.
cargo add slackblocks@2.7.0
cargo add serde_json
Build a message
use slackblocks::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let message = MessagePayload::builder("C01234567")
.text("Hello from slackblocks!")
.block(SectionBlock::builder().text("Hello, world!").build()?)
.build()?;
println!("{}", serde_json::to_string(&message)?);
Ok(())
}
Builders consume themselves and return Result from build(). Use ? to
propagate ValidationError. Built values own their data; getters borrow it.
text on the message is Slack's notification and accessibility fallback.
Install the package
PHP 8.2 or newer is required, on a 64-bit build with the JSON and PCRE extensions. The core has no third-party runtime dependencies. Composer 2 handles PSR-4 autoloading.
PHP support begins with coordinated version 2.7.0. Install the Packagist package:
composer require nicklambourne/slackblocks:^2.7
Stable installs download a PHP-only archive. Composer's --prefer-source option
checks out the monorepo and is required for unreleased development branches.
Keep vendor/autoload.php in your application's entry point and use
declare(strict_types=1) in your calling PHP files. PHP 8.2's named arguments,
readonly classes and backed enums are the public API; named parameter spelling
is part of compatibility.
Build a message
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Slackblocks as S;
$message = new S\MessagePayload(
channel: 'C0123456789',
text: 'Build #482 passed',
blocks: [
new S\HeaderBlock('Build #482 passed'),
new S\SectionBlock(fields: ["*Branch*\n`main`", "*Tests*\n1,247 passed"]),
new S\ActionsBlock(elements: [
new S\ButtonElement(text: 'View build', actionId: 'view', url: 'https://ci.example.com/482'),
]),
],
);
echo $message->toJson();
Send the resulting payload with JoliCode, cURL, or Laravel.
Install
Elixir 1.16.3+, compatible OTP 26.2+ and a 64-bit runtime are required.
Install {:slackblocks, "~> 2.8"} from Hex; follow Installation
for setup and the existing-Jason rebuild step.
Build a message
alias Slackblocks.{Message, Blocks, Elements}
message = Message.new!(
channel: "C0123456789",
text: "Build #482 passed",
blocks: [
Blocks.Header.new!(text: "Build #482 passed"),
Blocks.Section.new!(fields: ["*Branch*\n`main`", "*Tests*\n1,247 passed"]),
Blocks.Actions.new!(elements: [
Elements.Button.new!(text: "View build", action_id: "view", url: "https://ci.example.com/482")
])
]
)
Slackblocks.to_map!(message)
Send the validated string-keyed map with the community Slack client or Req; see Sending Messages. Pattern matching, immutable edits and tagged errors are covered in the Recipe Book.
Where to go next
slackblocks constructs the Block Kit content while the established Slack client handles delivery. Continue with Sending Messages for more client options, or explore Using Blocks to build richer layouts.
For package-manager alternatives and environment details, see Installation.