Skip to main content

Getting Started with Bolt for Java

Bolt for Java is a framework on the JVM that offers an abstraction layer to build Slack apps using modern platform features.

This guide explains how to start your first Bolt app.

If you're not yet familiar with Slack app development in general, we recommend reading the Slack API docs.


Setting up your project

Let's start building a Slack app using Bolt! This guide includes instructions on how to set up a Bolt project with Maven and Gradle, so use whichever section you'd like.

Using Maven

After you create your Maven project, you need to add the bolt dependency to your pom.xml file. The bolt dependency is a framework-agnostic module. If you use Bolt along with Spring Boot, Quarkus (Undertow), or any others on top of the Servlet environment, the bolt-servlet library is required for your app. Adding only bolt-servlet also works.

To enable Socket Mode, the bolt-socket-mode library and its provided-scope dependencies are also required for your app.

<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt</artifactId>
<version>1.45.3</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt-socket-mode</artifactId>
<version>1.45.3</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>

By default, the tyrus-standalone-client dependency is used as the implementation to manage socket connections in the bolt-socket-mode artifact. If instead you would prefer to use the Java-WebSocket implementation, swap its artifact in instead of tyrus-standalone-client, and then set SocketModeClient.Backend.JavaWebSocket when initializing the client instance:

<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.1</version>
</dependency>

You will also need to ensure you set the compiler source and target to at least 1.8:

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Using Gradle

After you create your Gradle project, add the bolt dependencies to build.gradle.

dependencies {
implementation("com.slack.api:bolt-socket-mode:1.45.3")
implementation("javax.websocket:javax.websocket-api:1.1")
implementation("org.glassfish.tyrus.bundles:tyrus-standalone-client:1.20")
implementation("org.slf4j:slf4j-simple:1.7.36")
}

Running your Bolt app

Using bolt-socket-mode

bolt-socket-mode is a handy way to start your Socket Mode app. It allows developers to build a Slack app backend service by writing only a main method initializes App and establishes a WebSocket connection to the Socket Mode servers.

Using build.gradle

The following build settings should work as-is. Put it in the root directory of your project.

plugins {
id("application")
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.slack.api:bolt-socket-mode:1.45.3")
implementation("javax.websocket:javax.websocket-api:1.1")
implementation("org.glassfish.tyrus.bundles:tyrus-standalone-client:1.20")
implementation("org.slf4j:slf4j-simple:1.7.36")
}
application {
mainClassName = "hello.MyApp"
}
run {
// gradle run -DslackLogLevel=debug
systemProperty "org.slf4j.simpleLogger.log.com.slack.api", System.getProperty("slackLogLevel")
}

Using src/main/java/hello/MyApp.java

Only single source code is required to run your first Bolt app. You'll need to define the main method that starts SocketModeApp.

package hello;

import com.slack.api.bolt.App;
import com.slack.api.bolt.socket_mode.SocketModeApp;

public class MyApp {
public static void main(String[] args) throws Exception {
// App expects an env variable: SLACK_BOT_TOKEN
App app = new App();

app.command("/hello", (req, ctx) -> {
return ctx.ack(":wave: Hello!");
});

// SocketModeApp expects an env variable: SLACK_APP_TOKEN
new SocketModeApp(app).start();
}
}

If you go with JDK 10+, thanks to Local Variable Type Inference, you can write more concise code. To do so, install OpenJDK 11 and set the compatible Java versions in build.gradle as below, configuring the same on your IDE.

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

Now, you don't need to repeat the same type in a single line.

var app = new App();
app.command("/hello", (req, ctx) -> {
return ctx.ack(":wave: Hello!");
});
new SocketModeApp(app).start();

Environment variables

The default constructor expects the following two environment variables to exist when starting the app.

Env VariableDescription
SLACK_BOT_TOKENThe valid bot token value starting with xoxb- in your development workspace. To issue a bot token, install your Slack app that has a bot user to your development workspace. Visit the Slack app settings page, choose the app you're working on, and go to Settings > Install App on the left pane (Add app_mentions:read bot scope if you see the message saying "Please add at least one feature or permission scope to install your app.").

If you run an app that is installable for multiple workspaces, no need to specify this. Consult App Distribution (OAuth) for further information.
SLACK_SIGNING_SECRETThe secret value shared only with the Slack Platform. It is used for verifying incoming requests from Slack. Request verification is crucial for security as Slack apps have internet-facing endpoints. To know the value, visit the Slack app settings page, choose the app you're working on, go to Settings > Basic Information on the left pane, and find App Credentials > Signing Secret on the page. Refer to Verifying requests from Slack for more information.

If you prefer configuring an App in a different way, write some code to initialize AppConfig on your own.

Set the two environment variables and run:

  • Gradle users: gradle run (for more detailed logging: gradle run -DslackLogLevel=debug),
  • Maven users: mvn compile exec:java -Dexec.mainClass="hello.MyApp" (for more detailed logging you can also provide the -Dorg.slf4j.simpleLogger.defaultLogLevel=debug flag)

The command runs your main method.

# Visit https://api.slack.com/apps to know these
export SLACK_BOT_TOKEN=xoxb-...your-own-valid-one
export SLACK_SIGNING_SECRET=123abc...your-own-valid-one

# run the main function
# gradle users should run:
gradle run
# maven users should run:
mvn compile exec:java -Dexec.mainClass="hello.MyApp"

You will see the message saying "⚡️ Bolt app is running!" in stdout.

If you get stuck, go through the following checklist:

  • ✅ JDK 8 or higher installed (if not, run brew install openjdk@11 for macOS / visit OpenJDK website for others)
  • ✅ Gradle installed (if not, run brew install gradle for macOS / visit their website for others)
  • build.gradle has bolt-socket-mode and tyrus-standalone-client in the dependencies and valid application plugin settings
  • src/main/java/hello/MyApp.java with a class having its main method
  • Create a Slack App, add commands bot scope, add an app-level token with connections:write scope, and install the app to your development workspace
  • ✅ Copy Bot User OAuth Access Token and App-Level Token from your Slack App admin pages and set them to env variables

Enabling the /hello command

Your app is up now! However, the slash command /hello in the code is still unavailable. To enable it, follow the steps below:

  • Visit Slack app settings pages
  • Choose your app
  • Go to Settings > Socket Mode on the left pane
    • Turn on Enable Socket Mode
  • Go to Features > Slash Commands on the left pane
    • Click Create New Command button
    • Input the command information on the dialog:
      • Command: /hello
      • Short Description: whatever you like
    • Click Save Button

Now you can hit the /hello command in your development workspace. If your app is successfully running, the app should respond to the command by replying 👋 Hello!.

What about Spring Boot?

As Spring Boot is one of the most popular web frameworks in the Java world, you may be curious about the possibility to let this Bolt app live together with it.

Don't worry, we can inject Bolt into Spring Boot apps.

Add implementation("com.slack.api:bolt:1.45.3") to dependencies in build.gradle and write a few lines of code.

@Configuration
public class SlackApp {
@Bean
public App initSlackApp() {
App app = new App();
app.command("/hello", (req, ctx) -> ctx.ack("Hi there!"));
return app;
}
}

@WebServlet("/slack/events")
public class SlackAppController extends SlackAppServlet {
public SlackAppController(App app) {
super(app);
}
}

Check the detailed guide here for more information.


Getting started with Kotlin

For code simplicity, Kotlin is a great option for writing Bolt apps. In this section, you'll learn how to set up a Kotlin project for Bolt apps.

Using build.gradle

Most of the build settings are necessary for enabling Kotlin language. Adding bolt-socket-mode && tyrus-standalone-client to the dependencies is the only one that is specific to Bolt.

plugins {
id("org.jetbrains.kotlin.jvm") version "1.7.21"
id("application")
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.slack.api:bolt-socket-mode:1.45.3")
implementation("javax.websocket:javax.websocket-api:1.1")
implementation("org.glassfish.tyrus.bundles:tyrus-standalone-client:1.20")
implementation("org.slf4j:slf4j-simple:1.7.36") // or logback-classic
}
application {
mainClassName = "MyAppKt" // add "Kt" suffix for main function source file
}

If you're already familiar with Kotlin and prefer the Gradle Kotlin DSL, of course, there is nothing stopping you.

Using src/main/kotlin/MyApp.kt

Here is a minimum source file that starts a Bolt app on your local machine.

import com.slack.api.bolt.App
import com.slack.api.bolt.socket_mode.SocketModeApp

fun main() {
val app = App()

// Write some code here

SocketModeApp(app).start()
}

Running your Kotlin app

If all items from the checklist are ✅, bootstrapping your first Kotlin-flavored Bolt app will succeed:

# Visit https://api.slack.com/apps to know these
export SLACK_BOT_TOKEN=xoxb-...your-own-valid-one
export SLACK_SIGNING_SECRET=123abc...your-own-valid-one

# run the main function
gradle run

From here, you're ready to write code and restart the app. Enjoy Bolt app development in Kotlin! 👋

Tip

We strongly recommend using IntelliJ IDEA here even if you don't prefer using IDEs. The IDE is the smoothest way to try Kotlin application development.


Next steps

Read the Bolt Basics page for further information.

If you want to know ways to run a Bolt app with Spring Boot, Micronaut, Quarkus, or Helidon SE, refer to the Supported Web Frameworks page.

Also, many examples are available in the GitHub repository.