A Python implementation for uploading documents to Slack channels using Slack's modern REST API.
This project demonstrates how to use Slack's REST APIs to upload and share documents to Slack channels. It implements the modern three-step upload process that replaced the deprecated files.upload method.
As of May 16, 2024, Slack deprecated the files.upload API method in favor of a more asynchronous and scalable approach. The new process involves three steps:
API Method: files.getUploadURLExternal
Request an upload URL and file ID from Slack:
curl --location 'https://slack.com/api/files.getUploadURLExternal' \
--header 'Authorization: Bearer {token}' \
--form 'filename="document.pdf"' \
--form 'length="12345"'Response:
{
"ok": true,
"upload_url": "https://files.slack.com/upload/v1/...",
"file_id": "F012AB3CDE4"
}Upload the actual file content to the URL received in Step 1:
curl --location 'https://files.slack.com/upload/v1/...' \
--header 'Content-Type: application/octet-stream' \
--data-binary '@document.pdf'API Method: files.completeUploadExternal
Finalize the upload and optionally share to a channel:
curl --location 'https://slack.com/api/files.completeUploadExternal' \
--header 'Authorization: Bearer {token}' \
--form 'files="[{\"title\":\"My Document\", \"id\":\"F012AB3CDE4\"}]"' \
--form 'channel_id="C0123456789"' \
--form 'initial_comment="Here is the document"'Your Slack app must have the following OAuth scopes:
files:write- Required to upload filesfiles:read- Optional, for reading file information
- Python 3.7 or higher
- A Slack workspace where you have permission to create apps
- A Slack app with the
files:writescope
- Go to https://api.slack.com/apps
- Click "Create New App" → "From scratch"
- Enter an app name (e.g., "Document Uploader") and select your workspace
- Click "Create App"
- In your app settings, go to "OAuth & Permissions"
- Scroll to "Scopes" → "Bot Token Scopes"
- Add the following scopes:
files:writechat:write(optional, if you want to post messages)
- Scroll to the top and click "Install to Workspace"
- Authorize the app
- Copy the "Bot User OAuth Token" (starts with
xoxb-)
- Open Slack in your browser
- Navigate to the channel where you want to upload files
- The channel ID is in the URL:
https://app.slack.com/client/T.../C...- The part starting with
Cis your channel ID (e.g.,C0123456789)
- The part starting with
Alternatively, right-click the channel → "View channel details" → scroll to bottom to see the Channel ID.
# Clone or download this repository
cd "Slack Assignment"
# Install required packages
pip install -r requirements.txt-
Copy the example environment file:
cp .env.example .env
-
Edit
.envand add your credentials:SLACK_BOT_TOKEN=xoxb-your-actual-token-here SLACK_CHANNEL_ID=C0123456789
Upload a file to the configured channel:
python slack_file_uploader.py path/to/your/document.pdfUpload a file with a custom title:
python slack_file_uploader.py resume.pdf "My Resume"Upload a file with a title and initial comment:
python slack_file_uploader.py resume.pdf "My Resume" "Here is my updated resume for review"📤 Uploading file: resume.pdf (245678 bytes)
Step 1/3: Getting upload URL...
✓ Received upload URL and file ID: F012AB3CDE4
Step 2/3: Uploading file content...
✓ File content uploaded successfully
Step 3/3: Completing upload...
✓ Upload completed successfully
✅ File shared to channel: C0123456789
📋 Upload Details:
File ID: F012AB3CDE4
Name: resume.pdf
Title: My Resume
Size: 245678 bytes
Type: PDF
Permalink: https://yourworkspace.slack.com/files/...
The main script contains the SlackFileUploader class with the following methods:
get_upload_url(filename, file_size)- Step 1: Request upload URL from Slackupload_file_content(upload_url, file_path)- Step 2: Upload file to the URLcomplete_upload(file_id, title, channel_id, initial_comment)- Step 3: Finalize and shareupload_file(file_path, title, channel_id, initial_comment)- Convenience method combining all steps
You can also import and use the SlackFileUploader class in your own code:
from slack_file_uploader import SlackFileUploader
# Initialize uploader
uploader = SlackFileUploader(token="xoxb-your-token")
# Upload file
result = uploader.upload_file(
file_path="document.pdf",
title="Important Document",
channel_id="C0123456789",
initial_comment="Please review this document"
)
print(f"File uploaded: {result['files'][0]['permalink']}")- Never commit tokens - The
.gitignorefile excludes.envto prevent accidental commits - Use environment variables - Store sensitive credentials in
.envfile - Limit token scopes - Only request the scopes your app needs
- Rotate tokens regularly - Regenerate tokens periodically
- Use Bot tokens - Bot tokens (
xoxb-) are safer than user tokens
- Make sure you created a
.envfile with your token - Verify the token starts with
xoxb-
- Your app needs the
files:writescope - Go to your app settings → OAuth & Permissions → Add the scope
- Reinstall the app to your workspace
- Verify the channel ID is correct
- Make sure the bot is added to the channel (invite it with
/invite @YourBot)
- Your token may be expired or invalid
- Regenerate the token in your app settings
- Make sure you're providing the
channel_idparameter - Verify the bot has been invited to the channel
- Maximum file size: 1GB for free workspaces, larger for paid plans
- Rate limits apply (Tier 3: 20+ requests per minute)
- Files are scanned for malware before being made available
This project is provided as-is for educational and demonstration purposes.