Skip to content

feat: add slack api command#538

Open
mwbrooks wants to merge 7 commits into
mainfrom
mwbrooks-api-command
Open

feat: add slack api command#538
mwbrooks wants to merge 7 commits into
mainfrom
mwbrooks-api-command

Conversation

@mwbrooks
Copy link
Copy Markdown
Member

@mwbrooks mwbrooks commented May 10, 2026

Changelog

Added slack api <method> [key=value ...] [flags] command for calling any Slack API method directly from the Slack CLI.

Examples:

  • slack api chat.postMessage channel=C123 text="hi"
  • slack api chat.postMessage '{"channel":"C123","text":"hi"}'

Summary

Adds a new top-level slack api command that lets users call any Slack API method without leaving the terminal.

Key features:

  • Token resolution
    1. --token flag
    2. --app flag
    3. SLACK_BOT_TOKEN env var
    4. SLACK_USER_TOKEN env var
    5. App Prompt (only in project)
  • Body Format Auto-detection:
    • Form-Encoded: slack api chat.postMessage channel=C123 text="hi"
    • JSON: slack api chat.postMessage '{"channel":"C123","text":"hi"}'
  • Body Format Flags: --json <string> and --data <string>
    • Form-Encoded: slack api chat.postMessage --data 'channel=C123 text="hi"'
    • JSON: slack api chat.postMessage --json '{"channel":"C123","text":"hi"}'
  • HTTP Options Flags
    • Method override (-X)
    • Custom headers (-H)
    • Show Response headers (--include)
  • Output formatting
    • Pretty-printed JSON in TTY
    • Compact for piped output / scripting
  • Retry support
    • Automatic retry on 429/503 with Retry-After

Open Questions

  • I originally had support for a team prompt, but removed it because it uses our Slack CLI tooling token, which has limited scope access. Sounds good?

Preview

$ slack api auth.test --token xoxb-...
{
    "ok": true,
    "url": "https://myworkspace.slack.com/",
    "team": "My Workspace",
    "user": "bot",
    "team_id": "T12345",
    "user_id": "U12345"
}

Post Message as Form Data (with app selection prompt):

2026-05-10-slack-cli-api-1.mov

Post Message as JSON (with app selection prompt):

2026-05-10-slack-cli-api-2.mov

Post Message with Custom Token --token:

2026-05-10-slack-cli-api-3.mov

JSON Responses are Pretty-Printed in TTY:

2026-05-10-slack-cli-api-5.mov

JSON Responses are Condense in Scripting/Non-TTY:

2026-05-10-slack-cli-api-4.mov

--include All Response Headers:

2026-05-10-slack-cli-api-6.mov

Testing

  • slack api --help - shows correct token priority and examples
  • slack api auth.test --token xoxb-... - returns auth info
  • slack api auth.test --app local - installs app and uses bot token (in project)
  • SLACK_BOT_TOKEN=xoxb-... slack api auth.test - uses env var (outside project)
  • slack api auth.test - prompts interactively when no flags/env set (in project)
  • slack api chat.postMessage channel=C123 text="hi" - sends form-encoded body
  • slack api chat.postMessage '{"channel":"C123","text":"hi"}' - auto-detects JSON
  • slack api auth.test --include - shows HTTP status and response headers
  • slack api auth.test | cat - outputs compact JSON (non-TTY)
  • go test ./cmd/api/... ./internal/api/... - all unit tests pass

Requirements

Adds a new top-level `slack api <method> [key=value ...] [flags]` command
that calls any Slack API method with automatic token resolution, body
format detection, and response formatting.

Token resolution priority: --token flag, --app/--team flags (via
AppSelectPrompt in project), SLACK_BOT_TOKEN env, SLACK_USER_TOKEN env,
interactive prompt fallback.

Supports form-encoded key=value params, JSON auto-detection, --json and
--data flags, custom headers (-H), HTTP method override (-X), response
header display (--include), and TTY-aware pretty printing.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

❌ Patch coverage is 81.46552% with 43 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.52%. Comparing base (59d720d) to head (d7a032c).

Files with missing lines Patch % Lines
cmd/api/api.go 86.20% 13 Missing and 11 partials ⚠️
internal/api/raw_request.go 66.66% 11 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #538      +/-   ##
==========================================
+ Coverage   71.30%   71.52%   +0.21%     
==========================================
  Files         222      224       +2     
  Lines       18695    18927     +232     
==========================================
+ Hits        13331    13537     +206     
- Misses       4184     4190       +6     
- Partials     1180     1200      +20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mwbrooks mwbrooks self-assigned this May 10, 2026
@mwbrooks mwbrooks added enhancement M-T: A feature request for new functionality changelog Use on updates to be included in the release notes semver:minor Use on pull requests to describe the release version increment labels May 10, 2026
@mwbrooks mwbrooks added this to the Next Release milestone May 10, 2026
mwbrooks added 5 commits May 10, 2026 16:46
Token resolution now errors if no token is found rather than falling
back to a workspace selection prompt. Users must provide a token via
--token, --app, or SLACK_BOT_TOKEN/SLACK_USER_TOKEN env vars.
@mwbrooks mwbrooks marked this pull request as ready for review May 11, 2026 01:25
@mwbrooks mwbrooks requested a review from a team as a code owner May 11, 2026 01:25
Copy link
Copy Markdown
Contributor

@srtaalej srtaalej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! ⭐ thanks for adding the test commands in the description 🫂
the command is working great except

lack api auth.test | cat

the test implies there should be JSON output but slack api auth.test is an interactive prompt so it errors out instead. perhaps we update the pr description? the command should be
lack api auth.test --token xoxb-... | cat
lack api auth.test --app A... | cat
right?

attempts++
if attempts == 1 {
w.Header().Set("Retry-After", "1")
w.WriteHeader(http.StatusTooManyRequests)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: does this cover retries on both 429 and 503 returns 🤔

Copy link
Copy Markdown
Contributor

@srtaalej srtaalej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left 2 testing-related comments/ questions but this is working great ! ⭐ ⭐ ⭐ such a handy command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog Use on updates to be included in the release notes enhancement M-T: A feature request for new functionality semver:minor Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants