Skip to main content

nautilus_kalshi/common/
urls.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Base URL resolution for Kalshi REST and WebSocket endpoints.
17
18use super::enums::KalshiEnvironment;
19
20const KALSHI_PRODUCTION_HTTP_URL: &str = "https://external-api.kalshi.com/trade-api/v2";
21const KALSHI_PRODUCTION_WS_URL: &str = "wss://external-api-ws.kalshi.com/trade-api/ws/v2";
22
23const KALSHI_DEMO_HTTP_URL: &str = "https://external-api.demo.kalshi.co/trade-api/v2";
24const KALSHI_DEMO_WS_URL: &str = "wss://external-api-ws.demo.kalshi.co/trade-api/ws/v2";
25
26/// Returns the REST base URL for the given environment.
27#[must_use]
28pub const fn kalshi_http_base_url(environment: KalshiEnvironment) -> &'static str {
29    match environment {
30        KalshiEnvironment::Production => KALSHI_PRODUCTION_HTTP_URL,
31        KalshiEnvironment::Demo => KALSHI_DEMO_HTTP_URL,
32    }
33}
34
35/// Returns the WebSocket URL for the given environment.
36#[must_use]
37pub const fn kalshi_ws_url(environment: KalshiEnvironment) -> &'static str {
38    match environment {
39        KalshiEnvironment::Production => KALSHI_PRODUCTION_WS_URL,
40        KalshiEnvironment::Demo => KALSHI_DEMO_WS_URL,
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use rstest::rstest;
47
48    use super::*;
49
50    #[rstest]
51    fn test_http_base_url() {
52        assert_eq!(
53            kalshi_http_base_url(KalshiEnvironment::Production),
54            KALSHI_PRODUCTION_HTTP_URL,
55        );
56        assert_eq!(
57            kalshi_http_base_url(KalshiEnvironment::Demo),
58            KALSHI_DEMO_HTTP_URL,
59        );
60    }
61
62    #[rstest]
63    fn test_ws_url() {
64        assert_eq!(
65            kalshi_ws_url(KalshiEnvironment::Production),
66            KALSHI_PRODUCTION_WS_URL,
67        );
68        assert_eq!(kalshi_ws_url(KalshiEnvironment::Demo), KALSHI_DEMO_WS_URL,);
69    }
70}