Download Custom Slack Emojis

This post will demonstrate how to use Slack’s emoji.list API method to extract and export custom emojis from Slack using the web client. This API is called by the web client to fetch all emojis when initializing a workspace, and can be called with a user’s xoxc token.

tl;dr

Instructions for downloading the emojis can be found in the Gist at:

https://gist.github.com/b17zr/6a6abf04f52464eec71603b747ffc49a

Details

API Call

When Slack first loads a channel, it fetches custom emojis for the workspace using the emoji.list API. These values do not appear to be stored in a JavaScript variable (although at one point they were…), but it is possible to re-call the API.

The documentation shows that a “token” argument is required for authentication. During normal use, the web client sends the xoxc token, which is saved in localStorage. The following JavaScript will extract this token:

JSON.parse(localStorage.localConfig_v2).teams[slackDebug.activeTeamId].token

The token will follow the format xoxc-11111111111-121212121212-121212121212-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef.

The API can be called using the multipart/form-data Content-Type. The following JavaScript will make the API request and dump the response to the console:

// get auth token for API request
var authToken = JSON.parse(localStorage.localConfig_v2).teams[slackDebug.activeTeamId].token;

// setup request
var formData = new FormData();
formData.append('token', authToken);

// make request
(async () => {
  const rawResponse = await fetch('/api/emoji.list', {
    method: 'POST',
    body: formData
  });

  const emojisApi = await rawResponse.json();
  
  // dump to console
  console.log(emojisApi.emoji);
})();

Saving the Data

Due to the restricted (lack of) Access-Control-Allow-Origin header on emoji.slack-edge.com, it is not possible to download the emojis using JavaScript straight from a workspace (located on app.slack.com). Attempting to do so will result in a Cross-Origin Resource Sharing (CORS) error.

Therefore, the download.js script of the Gist saves the results of the API call to a CSV. The CSV will have two columns: one for the image name and the other for the URL. Paste this script into the browser’s console, accept the popup warning, and save the file.

Downloading the Emojis

Since custom emojis do not require authentication, it is possible to download them from outside the browser.

On Linux and Mac, the wget utility can be leveraged to easily download these files. Ensure the terminal is open in the same directory as the emojis.csv file, and run the following script (from download.sh):

for emoji_line in $(cat emojis.csv); do
  name=$(echo "$emoji_line" | cut -d, -f1)
  url=$(echo "$emoji_line" | cut -d, -f2)

  wget -O "$name" "$url"
done

On Windows, PowerShell can be used to download all emojis in the CSV. Run the following commands from the same folder as the emojis.csv file (from download.ps1, requires PowerShell 3):

$emojis = Import-Csv -Path .\emojis.csv -Header 'Name','Url'

foreach ($emoji in $emojis) {
  Invoke-WebRequest -Uri $emoji.Url -OutFile $emoji.Name
}

After running the scripts, all custom emojis from the Slack workspace should be present in the current directory/folder.