Accessing your private data#
This page explains how to access private MGnify data.
Recommended: keep an
.envfile withMG_USERandMG_PASSWORD(see .env.example in the repo). These are auto-loaded intomgnipy.MGnipyConfigvia pydantic settings.Alternatively, pass credentials directly when creating a config:
config = MGnipyConfig(mg_user="...", mg_password="...")and use that withmgnipy.MGnipyor resource proxies.
How the authentication works (sliding token):
mgnipy.MGnipyConfigtakes care of obtaining a sliding token (JWT) from the token_obtain_sliding endpoint of the MGnify API using your username/passwordSliding 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.MGnipyConfiginvolved areobtain_auth_token,verify_auth_token,refresh_auth_token, andresolve_auth_token.The resolved token is stored in
MGnipyConfig.auth_tokenfor the session and used for authenticated API requests.
Quick configuration examples:#
Option 1. (Recommended) Auto-loading from an .env file#
Use an
.envfile (recommended). See.env.example— variablesMG_USERandMG_PASSWORD.example
.envcontents:MG_USER=<your MGnify or ENA username> MG_PASSWORD=<your MGnify or ENA password>
The .env file and
MG_USERandMG_PASSWORDvariables will auto be detected via pydantic settings and stored safely inmgnipy.MGnipyConfig
# uncomment below if colab
# !pip install mgnipy
# if .env files then can just proceed as normal, for example in a notebook:
from mgnipy import MGnipy
MG = MGnipy() # will automatically look for .env file and load credentials if found
Option 1.5 If using a different filename than .env#
Or if you prefer an env file name with a different filename then .env
you can manually load your given file by passing its path to
dotenv.load_dotenvand then use
os.getenvto get out your MGnify user and pass variablesinitiate
mgnipy.MGnipyor resource-specificMGnifierinstances (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()