Accessing your private data#

This page explains how to access private MGnify data.


  • Recommended: keep an .env file with MG_USER and MG_PASSWORD (see .env.example in the repo). These are auto-loaded into mgnipy.MGnipyConfig via pydantic settings.

  • Alternatively, pass credentials directly when creating a config: config = MGnipyConfig(mg_user="...", mg_password="...") and use that with mgnipy.MGnipy or resource proxies.


    How the authentication works (sliding token):

    • mgnipy.MGnipyConfig takes care of obtaining a sliding token (JWT) from the token_obtain_sliding endpoint of the MGnify API using your username/password

    • Sliding JWT includes an Access token, shorter expiry, and Refresh token, longer expiry.

    • The Access token is checked for validity using the token_verify endpoint

    • If the Access token is expired but the Refresh token has not then a new Access token can be obtained using token_refresh_sliding

    • The high-level methods within mgnipy.MGnipyConfig involved are obtain_auth_token, verify_auth_token, refresh_auth_token, and resolve_auth_token.

    • The resolved token is stored in MGnipyConfig.auth_token for the session and used for authenticated API requests.



Quick configuration examples:#

Option 1.5 If using a different filename than .env#

Or if you prefer an env file name with a different filename then .env

  1. you can manually load your given file by passing its path to dotenv.load_dotenv

  2. and then use os.getenv to get out your MGnify user and pass variables

  3. initiate mgnipy.MGnipy or resource-specific MGnifier instances (e.g., mgnipy.proxies) with those login credentials like above

import os

from dotenv import load_dotenv

from mgnipy import MGnipyConfig

# load env variables from specific filename
load_dotenv("path/to/your-env-file")

# pass to config
config = MGnipyConfig(
    mg_user=os.getenv("MG_USER"), mg_password=os.getenv("MG_PASSWORD")
)

# pass config to MGnipy
MG = MGnipy(config=config)
# or directly to proxy
from mgnipy.proxies import Biomes

biomes = Biomes(config=config)

Option 2. Explicity Configure#

Manually pass the login credentials to mgnipy.MGnipy or resource-specific MGnifier instances (e.g., mgnipy.V2.proxies) at init.

# pass login credentials to config
config = MGnipyConfig(
    mg_user="this-is-a-fake-user-name", mg_password="this-is-a-fake-password"
)

# pass config to MGnipy
MG = MGnipy(config=config)

Option 3. Pass credentials interactively#

if no .env or not passed to MGnipy or proxies then when private endpoints called you will be prompted with an input window for user and then password. Such as for endpoints with only private data e.g. private_studies

# requires sliding authentication token using user and pass
my_studies = MG.private_studies

Then…#

You can continue the same process as you would with non-private resources

# previewing query
# my_studies.explain()
# getting a page
# my_studies.get()