Quick Start with mstr-rest-requests
Assuming you have a working MicroStrategy Library installation to point at:
Establishing a connection
Probably the easiest way to establish a connection is by explicitly logging in and out:
- class mstr.requests.MSTRRESTSession(base_url=None)[source]
Full-featured session for the MicroStrategy REST API.
Combines authentication, session management, project helpers, and serialisation into a single
requests.Sessionsubclass. Use this class directly when you need manual control over the session lifecycle, or preferAuthenticatedMSTRRESTSessionfor automatic login/logout via a context manager.- login(username: str | None = None, password: str | None = None, api_key: str | None = None, application_type: int = 8) Response
Log in to the MicroStrategy REST API.
Convenience alias for
post_login(). If no credentials are provided the session attempts an anonymous connection.- Parameters:
username – MicroStrategy username or API key.
password – Password for standard authentication.
api_key – API key for trusted authentication.
application_type – MicroStrategy application type identifier.
- Returns:
A
requests.Responsefor the login request.
- logout() None
Log out and close the current REST API session.
Convenience alias for
post_logout().
Establishing a connection with a context manager
- class mstr.requests.AuthenticatedMSTRRESTSession(base_url: str | Callable[[], str] | None = None, username: str | Callable[[], str] | None = None, password: str | Callable[[], str] | None = None, identity_token: str | Callable[[], str] | None = None, api_key: str | Callable[[], str] | None = None, application_type: int = 8)[source]
Context-managed session that logs in on entry and out on exit.
All credential parameters accept a
Credential– either a plain string or a zero-argument callable returning a string. Callables are resolved when the context manager is entered, not at construction time. This enables integration with secrets managers and other deferred-lookup strategies.Example:
with AuthenticatedMSTRRESTSession( base_url="https://env.example.com/api/", username=lambda: fetch_username(), password=lambda: fetch_password(), ) as session: session.get("projects")
- Parameters:
base_url – MicroStrategy REST API root URL (or callable).
username – Username for standard authentication (login mode 1).
password – Password for standard authentication.
identity_token – Token for delegated authentication (login mode -1).
api_key – API key for trusted authentication (login mode 4096).
application_type – MicroStrategy application type identifier.
AuthenticatedMSTRRESTSession accepts optional credential arguments. Use
username and password for standard auth, identity_token for
delegation, or api_key for trusted (API key) authentication. Example with
API key:
with AuthenticatedMSTRRESTSession(
base_url="https://your-server/MicroStrategyLibrary/api/",
api_key="your-api-key",
) as session:
session.get("projects")