menu
Guides

Sessions

Understanding Sessions

Sessions can be used to manage an authentication token to external APIs. They can be used globally to set credentials for the entire process, or as variables, to allow multiple sessions to be used within a single process.

info

Note

The following examples use GsSession, but the same methods apply to all Session objects.

Creating a Session

A new session can be created and initialized by running:

from gs_quant.session import GsSession

session = GsSession.get()       # Get a new session
session.init()                  # Authenticate

The parameters required will depend on the authentication requirements of the external API. A session can be held prior to initialization to control when the authentication request is made. There are two ways to use a session:

  • Set as global
  • Use within a block

Initializing a session will authenticate against the API and hold a token which is reused automatically across subsequent requests. This is done via the init() function.

Global Sessions

Any request which is not scoped will use the global session automatically. The global session can be accessed through current:

session = GsSession.current     # Get current session
GsSession.current = session     # Set current session

The use() method creates, initializes and sets the session in one command:

GsSession.use()      # Create and set session

Scoped Sessions

Session objects can be used within blocks. This allows the same token to be shared by multiple requests within a defined scope. It also allows multiple environments or credentials to be used within the same process:

from gs_quant.session import Environment

prod_session = GsSession.get(Environment.PROD)      # Get PROD session
qa_session = GsSession.get(Environment.QA)          # Get QA session

with prod_session:

    # perform production requests

with qa_session:

    # perform non-production requests

Closing Sessions

A session can be closed using the following command:

session.close()

Behavior of the close() method will depend on the underlying implementation, but will generally close any open HTTP or socket connections, as well as require re-authentication for further requests.


Related Content


Certain solutions and Institutional Services described herein are provided via our Marquee platform. The Marquee platform is for institutional and professional clients only. This site is for informational purposes only and does not constitute an offer to provide the Marquee platform services described, nor an offer to sell, or the solicitation of an offer to buy, any security. Some of the services and products described herein may not be available in certain jurisdictions or to certain types of clients. Please contact your Goldman Sachs sales representative with any questions. Any data or market information presented on the site is solely for illustrative purposes. There is no representation that any transaction can or could have been effected on such terms or at such prices. Please see https://www.goldmansachs.com/disclaimer/sec-div-disclaimers-for-electronic-comms.html for additional information.
Transaction Banking services are offered by Goldman Sachs Bank USA (“GS Bank”). GS Bank is a New York State chartered bank, a member of the Federal Reserve System and a Member FDIC.
GS DAP™ is owned and operated by Goldman Sachs. This site is for informational purposes only and does not constitute an offer to provide, or the solicitation of an offer to provide access to or use of GS DAP™. Any subsequent commitment by Goldman Sachs to provide access to and / or use of GS DAP™ would be subject to various conditions, including, amongst others, (i) satisfactory determination and legal review of the structure of any potential product or activity, (ii) receipt of all internal and external approvals (including potentially regulatory approvals); (iii) execution of any relevant documentation in a form satisfactory to Goldman Sachs; and (iv) completion of any relevant system / technology / platform build or adaptation required or desired to support the structure of any potential product or activity.
Mosaic is a service mark of Goldman Sachs & Co. LLC. This service is made available in the United States by Goldman Sachs & Co. LLC and outside of the United States by Goldman Sachs International, or its local affiliates in accordance with applicable law and regulations. Goldman Sachs International and Goldman Sachs & Co. LLC are the distributors of the Goldman Sachs Funds. Depending upon the jurisdiction in which you are located, transactions in non-Goldman Sachs money market funds are affected by either Goldman Sachs & Co. LLC, a member of FINRA, SIPC and NYSE, or Goldman Sachs International. For additional information contact your Goldman Sachs representative. Goldman Sachs & Co. LLC, Goldman Sachs International, Goldman Sachs Liquidity Solutions, Goldman Sachs Asset Management, L.P., and the Goldman Sachs funds available through Goldman Sachs Liquidity Solutions and other affiliated entities, are under the common control of the Goldman Sachs Group, Inc.
© 2024 Goldman Sachs. All rights reserved.