Create Channels

Learn to use Slack API calls to create channels and invite users to them.

We can use the Slack API to start conversationsConversations are where messaging takes place. Channels, private messages, and group messages are all types of conversations. and add users to those conversations. The Slack API is a useful tool for automating various tasks. For example, we can create a new channel whenever a new team or group is made in a project management system. Once an application opens a channel, it has many endpoints that it can use to take different actions. These actions include changing a channel’s name and archiving it.

Let’s look at the following endpoints in this lesson:

  1. conversations.create: This endpoint initiates a public or private channel-based conversation.
  2. conversations.invite: This endpoint invites the user(s) to a channel.

Create a new channel

To create a new channel, we access the https://slack.com/api/conversations.create endpoint. Besides the token, we need to provide a name for the channel.

Note: Make sure the channel names are written in lowercase and without spaces. You can use dashes (-) instead of spaces.

Request parameters

Some important query parameters for the conversations.create endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

name

string

required

This is the name of the channel that will be created.

is_private

bool

optional

This is set as true to create a private channel.


Let’s call the conversations.create endpoint. Click the “Run” button to create a new channel named temp_channel in our workspace.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/conversations.create";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
name: "temp_channel",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function createChannel() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
createChannel();

Let’s look at the highlighted lines in the code widget shown above:

  • Lines 10–12: We give a name, temp_channel, to the application to make it into a channel. We’ll use the extracted channel ID to invite ourselves to the application’s channel in the next code widget.

Response fields

A successful response contains a channel property with the details of the newly created channel.

Note: Visit this lesson to view the details of the channel object.

Invite a user to the channel

To invite a user to the channel, we access the https://slack.com/api/conversations.invite endpoint. We’ll need to specify the users (one or more) and the channel ID.

Request parameters

Some important query parameters for the conversations.invite endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

channel

string

required

This parameter specifies the channel to which the message will be posted. It can be a public channel, a private group, or an IM channel (direct message). The value for this parameter can either be an encoded ID or a name of a channel.

users

string

required

This is the list of users IDs.

Run the code widget below to add yourself to a new channel.

Let’s call the conversations.invite endpoint. Click the “Run” button to add ourselves to the channel the application made in the previous code widget.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/conversations.invite";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID_APP}}",
users: "{{USER_ID}}",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function inviteUser() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
inviteUser();

Let’s look at the highlighted lines in the code widget shown above:

  • Lines 10–13: We specify the channel as the extracted channel ID from the previous code widget and set the users parameter to our user ID.

Response fields

The response from this endpoint is identical to the response from the conversations.create endpoint. It contains a channel property with the details of the channel to which the user was invited.

Note: Visit this lesson to view the details of the channel object.