This project provides a simple API endpoint for handling Slack OAuth 2.0 authentication and storing incoming webhook URLs for your workspace. The main logic is in api/slack/oauth.js.
- Handles Slack OAuth 2.0 flow
- Retrieves and logs incoming webhook URLs, team names, and app/team IDs
- (Commented) Example code for saving webhooks to a JSON file and SQLite database
- Redirects users to their Slack workspace after successful authentication
npm installCreate a .env file in the root directory with the following variables:
SLACK_CLIENT_ID=your_slack_client_id
SLACK_CLIENT_SECRET=your_slack_client_secret
SLACK_REDIRECT_URI=https://yourdomain.com/api/slack/oauth
- Replace
your_slack_client_idandyour_slack_client_secretwith values from your Slack app settings. - Set
SLACK_REDIRECT_URIto the public URL of your/api/slack/oauthendpoint.
Start your server (e.g., with Next.js or Express, depending on your setup):
npm run dev-
Create a Slack App
- Go to Slack API: Your Apps and click "Create New App".
- Choose "From scratch" and give your app a name and workspace.
-
Configure OAuth & Permissions
- In your app settings, go to OAuth & Permissions.
- Add a Redirect URL:
https://yourdomain.com/api/slack/oauth - Click "Save URLs".
- Add the required OAuth scopes (e.g.,
incoming-webhook,chat:write, etc.) depending on your app's needs.
-
Install the App to Your Workspace
- In the Slack app settings, click "Install App to Workspace".
- Authorize the app. You will be redirected to your app's
/api/slack/oauthendpoint.
-
Get Your Client ID and Secret
- In the app settings, under Basic Information, copy the Client ID and Client Secret.
- Add these to your
.envfile as shown above.
- Direct users to the Slack OAuth URL:
https://slack.com/oauth/v2/authorize?client_id=YOUR_CLIENT_ID&scope=incoming-webhook&redirect_uri=YOUR_REDIRECT_URI - After authorization, Slack will redirect to your
/api/slack/oauthendpoint with acodeparameter. - The handler exchanges the code for tokens and (optionally) stores the webhook URL and team info.
- Users are redirected to their Slack workspace.
curl -X POST -H 'Content-type: application/json' --data '{"text":"TOP!"}' https://hooks.slack.com/services/{{generated_webhook_url}}The code includes commented-out examples for saving webhook URLs to a JSON file and a SQLite database in the data/ directory. Uncomment and adapt these sections as needed for your persistence requirements.
- Ensure your redirect URI matches exactly in both your Slack app settings and your
.envfile. - Check your server logs for errors during the OAuth exchange.
- Make sure your server is accessible from the public internet for Slack to redirect users.