From 5066d1561ede07f781986d192f2ef9ee0141219a Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Wed, 2 Dec 2026 22:48:47 -0800 Subject: [PATCH 1/4] feat: default the client base_url from SLACK_API_URL Co-Authored-By: Claude --- slack_bolt/app/app.py | 4 +++- slack_bolt/app/async_app.py | 4 +++- slack_bolt/util/async_utils.py | 7 +++++- slack_bolt/util/utils.py | 7 +++++- tests/scenario_tests/test_app.py | 30 ++++++++++++++++++++++++++ tests/scenario_tests_async/test_app.py | 27 +++++++++++++++++++++++ 6 files changed, 75 insertions(+), 4 deletions(-) diff --git a/slack_bolt/app/app.py b/slack_bolt/app/app.py index cbaa340f7..932073790 100644 --- a/slack_bolt/app/app.py +++ b/slack_bolt/app/app.py @@ -171,7 +171,8 @@ def message_hello(message, say): signing_secret: The Signing Secret value used for verifying requests from Slack. token: The bot/user access token required only for single-workspace app. token_verification_enabled: Verifies the validity of the given token if True. - client: The singleton `slack_sdk.WebClient` instance for this app. + client: The singleton `slack_sdk.WebClient` instance for this app. When absent, the created client's + `base_url` defaults to the `SLACK_API_URL` environment variable if set. before_authorize: A global middleware that can be executed right before authorize function authorize: The function to authorize an incoming request from Slack by checking if there is a team/user in the installation data. @@ -239,6 +240,7 @@ def message_hello(message, say): self._framework_logger.warning(warning_client_prioritized_and_token_skipped()) else: self._client = create_web_client( + base_url=os.environ.get("SLACK_API_URL") or WebClient.BASE_URL, # NOTE: the token here can be None token=token, logger=self._framework_logger, diff --git a/slack_bolt/app/async_app.py b/slack_bolt/app/async_app.py index 729d626c2..4afa15e49 100644 --- a/slack_bolt/app/async_app.py +++ b/slack_bolt/app/async_app.py @@ -179,7 +179,8 @@ async def message_hello(message, say): # async function the built-in handler, which pints warning logs and returns 404 to Slack (Default: False) signing_secret: The Signing Secret value used for verifying requests from Slack. token: The bot/user access token required only for single-workspace app. - client: The singleton `slack_sdk.web.async_client.AsyncWebClient` instance for this app. + client: The singleton `slack_sdk.web.async_client.AsyncWebClient` instance for this app. When absent, + the created client's `base_url` defaults to the `SLACK_API_URL` environment variable if set. before_authorize: A global middleware that can be executed right before authorize function authorize: The function to authorize an incoming request from Slack by checking if there is a team/user in the installation data. @@ -244,6 +245,7 @@ async def message_hello(message, say): # async function self._framework_logger.warning(warning_client_prioritized_and_token_skipped()) else: self._async_client = create_async_web_client( + base_url=os.environ.get("SLACK_API_URL") or AsyncWebClient.BASE_URL, # NOTE: the token here can be None token=token, logger=self._framework_logger, diff --git a/slack_bolt/util/async_utils.py b/slack_bolt/util/async_utils.py index 858583063..81ab3f326 100644 --- a/slack_bolt/util/async_utils.py +++ b/slack_bolt/util/async_utils.py @@ -6,8 +6,13 @@ from slack_bolt.version import __version__ as bolt_version -def create_async_web_client(token: Optional[str] = None, logger: Optional[Logger] = None) -> AsyncWebClient: +def create_async_web_client( + token: Optional[str] = None, + logger: Optional[Logger] = None, + base_url: str = AsyncWebClient.BASE_URL, +) -> AsyncWebClient: return AsyncWebClient( + base_url=base_url, token=token, logger=logger, user_agent_prefix=f"Bolt-Async/{bolt_version}", diff --git a/slack_bolt/util/utils.py b/slack_bolt/util/utils.py index 9ee313821..c4ef4f732 100644 --- a/slack_bolt/util/utils.py +++ b/slack_bolt/util/utils.py @@ -11,8 +11,13 @@ from slack_bolt.version import __version__ as bolt_version -def create_web_client(token: Optional[str] = None, logger: Optional[Logger] = None) -> WebClient: +def create_web_client( + token: Optional[str] = None, + logger: Optional[Logger] = None, + base_url: str = WebClient.BASE_URL, +) -> WebClient: return WebClient( + base_url=base_url, token=token, logger=logger, user_agent_prefix=f"Bolt/{bolt_version}", diff --git a/tests/scenario_tests/test_app.py b/tests/scenario_tests/test_app.py index 5cdff39bf..9cfd0bdca 100644 --- a/tests/scenario_tests/test_app.py +++ b/tests/scenario_tests/test_app.py @@ -103,6 +103,36 @@ def test_socket_mode_app_without_signing_secret(self): ) assert app is not None + def test_slack_api_url_env_sets_base_url(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/") + app = App( + signing_secret="valid", + token="xoxb-valid", + token_verification_enabled=False, + ) + assert app.client.base_url == "https://qa.slack.com/api/" + + def test_slack_api_url_env_skipped_for_given_client(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/") + app = App( + signing_secret="valid", + client=WebClient( + token="xoxb-valid", + base_url="https://custom.slack.com/api/", + ), + token_verification_enabled=False, + ) + assert app.client.base_url == "https://custom.slack.com/api/" + + def test_slack_api_url_env_empty_keeps_default_base_url(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "") + app = App( + signing_secret="valid", + token="xoxb-valid", + token_verification_enabled=False, + ) + assert app.client.base_url == "https://slack.com/api/" + # -------------------------- # multi teams auth # -------------------------- diff --git a/tests/scenario_tests_async/test_app.py b/tests/scenario_tests_async/test_app.py index 6f3fb34f8..c29c2156f 100644 --- a/tests/scenario_tests_async/test_app.py +++ b/tests/scenario_tests_async/test_app.py @@ -79,6 +79,33 @@ def test_token_absence(self): with pytest.raises(BoltError): AsyncApp(signing_secret="valid", token="") + def test_slack_api_url_env_sets_base_url(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/") + app = AsyncApp( + signing_secret="valid", + token="xoxb-valid", + ) + assert app.client.base_url == "https://qa.slack.com/api/" + + def test_slack_api_url_env_skipped_for_given_client(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/") + app = AsyncApp( + signing_secret="valid", + client=AsyncWebClient( + token="xoxb-valid", + base_url="https://custom.slack.com/api/", + ), + ) + assert app.client.base_url == "https://custom.slack.com/api/" + + def test_slack_api_url_env_empty_keeps_default_base_url(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "") + app = AsyncApp( + signing_secret="valid", + token="xoxb-valid", + ) + assert app.client.base_url == "https://slack.com/api/" + # -------------------------- # multi teams auth # -------------------------- From 724188cb58411dc2dd93a57eed97bcf58b4dcd0b Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Thu, 3 Dec 2026 07:48:43 -0800 Subject: [PATCH 2/4] refactor: resolve SLACK_API_URL in the shared client helpers Co-authored-by: William Bergamin <25348381+WilliamBergamin@users.noreply.github.com> Co-Authored-By: Claude --- slack_bolt/app/app.py | 1 - slack_bolt/app/async_app.py | 1 - slack_bolt/util/async_utils.py | 5 +++-- slack_bolt/util/utils.py | 5 +++-- tests/slack_bolt/oauth/test_oauth_flow.py | 13 +++++++++++++ .../oauth/test_async_oauth_flow.py | 15 +++++++++++++++ 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/slack_bolt/app/app.py b/slack_bolt/app/app.py index 932073790..0389f9a91 100644 --- a/slack_bolt/app/app.py +++ b/slack_bolt/app/app.py @@ -240,7 +240,6 @@ def message_hello(message, say): self._framework_logger.warning(warning_client_prioritized_and_token_skipped()) else: self._client = create_web_client( - base_url=os.environ.get("SLACK_API_URL") or WebClient.BASE_URL, # NOTE: the token here can be None token=token, logger=self._framework_logger, diff --git a/slack_bolt/app/async_app.py b/slack_bolt/app/async_app.py index 4afa15e49..4913bbd15 100644 --- a/slack_bolt/app/async_app.py +++ b/slack_bolt/app/async_app.py @@ -245,7 +245,6 @@ async def message_hello(message, say): # async function self._framework_logger.warning(warning_client_prioritized_and_token_skipped()) else: self._async_client = create_async_web_client( - base_url=os.environ.get("SLACK_API_URL") or AsyncWebClient.BASE_URL, # NOTE: the token here can be None token=token, logger=self._framework_logger, diff --git a/slack_bolt/util/async_utils.py b/slack_bolt/util/async_utils.py index 81ab3f326..61c8e08ac 100644 --- a/slack_bolt/util/async_utils.py +++ b/slack_bolt/util/async_utils.py @@ -1,3 +1,4 @@ +import os from logging import Logger from typing import Optional @@ -9,10 +10,10 @@ def create_async_web_client( token: Optional[str] = None, logger: Optional[Logger] = None, - base_url: str = AsyncWebClient.BASE_URL, + base_url: Optional[str] = None, ) -> AsyncWebClient: return AsyncWebClient( - base_url=base_url, + base_url=base_url or os.environ.get("SLACK_API_URL") or AsyncWebClient.BASE_URL, token=token, logger=logger, user_agent_prefix=f"Bolt-Async/{bolt_version}", diff --git a/slack_bolt/util/utils.py b/slack_bolt/util/utils.py index c4ef4f732..2b5cd657e 100644 --- a/slack_bolt/util/utils.py +++ b/slack_bolt/util/utils.py @@ -1,5 +1,6 @@ import copy import inspect +import os import sys from logging import Logger from typing import Optional, Union, Dict, Any, Sequence, Callable, List @@ -14,10 +15,10 @@ def create_web_client( token: Optional[str] = None, logger: Optional[Logger] = None, - base_url: str = WebClient.BASE_URL, + base_url: Optional[str] = None, ) -> WebClient: return WebClient( - base_url=base_url, + base_url=base_url or os.environ.get("SLACK_API_URL") or WebClient.BASE_URL, token=token, logger=logger, user_agent_prefix=f"Bolt/{bolt_version}", diff --git a/tests/slack_bolt/oauth/test_oauth_flow.py b/tests/slack_bolt/oauth/test_oauth_flow.py index 77258685d..255b4011b 100644 --- a/tests/slack_bolt/oauth/test_oauth_flow.py +++ b/tests/slack_bolt/oauth/test_oauth_flow.py @@ -44,6 +44,19 @@ def test_instantiation(self): assert oauth_flow.logger is not None assert oauth_flow.client is not None + def test_instantiation_slack_api_url_env(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/") + oauth_flow = OAuthFlow( + settings=OAuthSettings( + client_id="111.222", + client_secret="xxx", + scopes=["chat:write", "commands"], + installation_store=FileInstallationStore(), + state_store=FileOAuthStateStore(expiration_seconds=120), + ) + ) + assert oauth_flow.client.base_url == "https://qa.slack.com/api/" + def test_handle_installation_default(self): oauth_flow = OAuthFlow( settings=OAuthSettings( diff --git a/tests/slack_bolt_async/oauth/test_async_oauth_flow.py b/tests/slack_bolt_async/oauth/test_async_oauth_flow.py index 538861c05..3cafe6757 100644 --- a/tests/slack_bolt_async/oauth/test_async_oauth_flow.py +++ b/tests/slack_bolt_async/oauth/test_async_oauth_flow.py @@ -60,6 +60,21 @@ async def test_instantiation(self): assert oauth_flow.logger is not None assert oauth_flow.client is not None + @pytest.mark.asyncio + async def test_instantiation_slack_api_url_env(self, monkeypatch): + monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/") + oauth_flow = AsyncOAuthFlow( + settings=AsyncOAuthSettings( + client_id="111.222", + client_secret="xxx", + scopes=["chat:write", "commands"], + user_scopes=["search:read"], + installation_store=FileInstallationStore(), + state_store=FileOAuthStateStore(expiration_seconds=120), + ) + ) + assert oauth_flow.client.base_url == "https://qa.slack.com/api/" + @pytest.mark.asyncio async def test_scopes_as_str(self): settings = AsyncOAuthSettings( From b31749631a2a29cf1193775742d886edd6dfd283 Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Thu, 3 Dec 2026 12:10:34 -0800 Subject: [PATCH 3/4] refactor: drop the unused base_url param from the client helpers Co-authored-by: William Bergamin <25348381+WilliamBergamin@users.noreply.github.com> Co-Authored-By: Claude --- slack_bolt/util/async_utils.py | 8 ++------ slack_bolt/util/utils.py | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/slack_bolt/util/async_utils.py b/slack_bolt/util/async_utils.py index 61c8e08ac..c4914e8f5 100644 --- a/slack_bolt/util/async_utils.py +++ b/slack_bolt/util/async_utils.py @@ -7,13 +7,9 @@ from slack_bolt.version import __version__ as bolt_version -def create_async_web_client( - token: Optional[str] = None, - logger: Optional[Logger] = None, - base_url: Optional[str] = None, -) -> AsyncWebClient: +def create_async_web_client(token: Optional[str] = None, logger: Optional[Logger] = None) -> AsyncWebClient: return AsyncWebClient( - base_url=base_url or os.environ.get("SLACK_API_URL") or AsyncWebClient.BASE_URL, + base_url=os.environ.get("SLACK_API_URL") or AsyncWebClient.BASE_URL, token=token, logger=logger, user_agent_prefix=f"Bolt-Async/{bolt_version}", diff --git a/slack_bolt/util/utils.py b/slack_bolt/util/utils.py index 2b5cd657e..c3525b579 100644 --- a/slack_bolt/util/utils.py +++ b/slack_bolt/util/utils.py @@ -12,13 +12,9 @@ from slack_bolt.version import __version__ as bolt_version -def create_web_client( - token: Optional[str] = None, - logger: Optional[Logger] = None, - base_url: Optional[str] = None, -) -> WebClient: +def create_web_client(token: Optional[str] = None, logger: Optional[Logger] = None) -> WebClient: return WebClient( - base_url=base_url or os.environ.get("SLACK_API_URL") or WebClient.BASE_URL, + base_url=os.environ.get("SLACK_API_URL") or WebClient.BASE_URL, token=token, logger=logger, user_agent_prefix=f"Bolt/{bolt_version}", From 2816f13b88aff66092f1d3988e7294831c11055f Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Thu, 3 Dec 2026 12:10:35 -0800 Subject: [PATCH 4/4] docs: note the SLACK_API_URL client default on the web api page Co-Authored-By: Claude --- docs/english/concepts/web-api.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/english/concepts/web-api.md b/docs/english/concepts/web-api.md index 81f7c9b60..68d45f49e 100644 --- a/docs/english/concepts/web-api.md +++ b/docs/english/concepts/web-api.md @@ -4,6 +4,8 @@ You can call [any Web API method](/reference/methods) using the `WebClient` prov The token used to initialize Bolt can be found in the `context` object, which is required to call most Web API methods. +The `SLACK_API_URL` environment variable sets the `base_url` of the client that Bolt creates, with a trailing slash added when missing. A `client` passed to `App` or `AsyncApp` keeps its own `base_url`. + :::info[Refer to [the module document](https://docs.slack.dev/tools/bolt-python/reference/kwargs_injection/args.html) to learn the available listener arguments.] :::