A lightweight Python client for hassle-free tinkering with Saxo OpenAPI.
NOTE: This Python package was created by an enthusiast as a learning project. None of the contents in this repository are maintained by Saxo Bank, and Saxo Bank does not guarantee correctness of the provided implementation.
- Authentication and session management with Saxo SSO
- Supports OAuth 2.0
Code
grant type - Works seamlessly in both
SIM
andLIVE
environments - Automated handling of callback from SSO (optional)
- Headless authentication for deployed applications (optional)
- Keep sessions and active websockets alive by refreshing access tokens via asyncio
- Supports OAuth 2.0
- Read operations (
GET
requests) - Write operations (
POST
,PUT
,PATCH
,DELETE
requests) - Supports streaming and decoding of streaming messages
- Error handling with comprehensive exception messages
pip install saxo-apy
- Python 3.7+
- An OpenAPI application registered on Saxo Bank's Developer Portal
- Create a free developer account if you don't have one already.
- Ensure the application is set up with
Grant Type: Code
as authentication flow. - At least 1 localhost redirect needs to be defined such as
http://localhost:12321/redirect
(for development/testing purposes) - (Optional) enable trading permissions for the app
See Get Started for an in-depth example!
Copy your apps's config by clicking Copy App Object
on the Developer Portal app details page.
The client requires this dictionary to be provided when initializing:
from saxo_apy import SaxoOpenAPIClient
# copy app config here:
config = {
"AppName": "Your OpenAPI App",
"AppKey": "...",
"AuthorizationEndpoint": "...",
"TokenEndpoint": "...",
"GrantType": "Code",
"OpenApiBaseUrl": "...",
"RedirectUrls": [
"...
],
"AppSecret": "..."
}
client = SaxoOpenAPIClient(config)
client.login()
me = client.get("/port/v1/users/me")
print(me)
> {'ClientKey': 'U8SNV3JLdN4gzcQfmThXJA==',
> 'Culture': 'en-US',
> 'Language': 'en',
> ...
See the samples for loads more examples on how to use the client.
This package requires 4 dependencies:
pydantic
, for parsing config and JSON responsesFlask
, to run a local server and catch the callback from Saxo SSOhttpx
, for sending requests to OpenAPI and managing the client sessionwebsockets
, for setting up a websocket connection to Saxo's streaming serviceloguru
, to handle logging
The client supports OAuth Code flow and will automatically spin up a server to listen for the redirect from Saxo SSO. At least 1 localhost
redirect needs to be defined in application config for this purpose.
By default, the client will use the first available localhost redirect to run the server on (typically only 1 exists in the config).
The client validates redirect urls in application config automatically. OAuth 2.0 code flow requires a fixed port to be specified on the redirect url. In case this is incorrectly configured, an error message will guide you to ensure app config is correct with OpenAPI:
one or more redirect urls have no port configured, which is required for grant type 'Code' - ensure a port is configured in the app config object for each url (example: http://localhost:23432/redirect)