'How to create a shared channel via Slack conversation API?

I would like to programmatically create a channel in Slack, make it shared and get the invitatino link. I found two APIS: conversations.create and conversations.invite but they'r only applicable to channels within the workspace. Doesn't seem what I want to do is possible with the Slack APIs currently, but wanted to see if anybody had a work around.



Solution 1:[1]

You could use the conversations.inviteShared API method.

This Slack Connect API method creates an invitation to a Slack Connect channel. The invitation can be sent to users or bots in another workspace either as a shareable URL or as an email invite.

The invited user must be known to your app (i.e., a channel is already shared between the organization your app is installed on and the organization of the user).

If the channel to be joined is not already a Slack Connect channel, it becomes a Connect channel when you use this method to invite users from another workspace.

Here's what worked for me:

Step 1: Create a Slack Channel via Vanilla JS


  // Headers
  const headers = {
    'Content-type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer ' + yourAPIToken
  }

  // Create Slack channel
  let create = await fetch('https://slack.com/api/conversations.create', {
    method: 'POST',
    headers: headers,
    body: JSON.stringify({
      'team_id': 'CXXXXXXX' // Your team id,
      'is_private': true, // Optional. Create a private channel instead of a public one 
      'name': 'your-channel' // Your desired channel name
    })
  }).then(response => response.json());

console.log(create);

Step 2: Invite yourself (and/or default users)

This is important. Otherwise no one will see the channel.


// Invite default users
let invite = await fetch('https://slack.com/api/conversations.invite', {
    method: 'POST',
    headers: headers,
    body: JSON.stringify({
        channel: create.channel.id,
        users: 'XXXXXXXXX,XXXXXXXXX' // A comma separated list of user IDs.
      })
  }).then(response => response.json());

console.log(invite);

Step 3: Invite external users via email


// Invite default users
// NOTE: The body object wasn't accepted for some reason, so moved it to the URI instead.
let inviteShared = await fetch('https://slack.com/api/conversations.inviteShared?channel=' 
                      + create.channel.id + '&emails=' 
                      + encodeURIComponent('[email protected]'), {
            method: 'POST',
            headers: headers,
          }).then(response => response.json());

console.log(inviteShared);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wbq