The Sayari Python SDK provides convenient access to the Sayari API from applications written in Python.
Please see our docs site for more info and or to get in touch with us.
Before you begin using the Sayari API, it is essential to set up a Python virtual environment, install the required dependencies from requirements.txt, and configure the necessary environment variables using a .env file.
These steps ensure your environment is correctly isolated and properly configured for using the Sayari API.
To keep your dependencies isolated, create and activate a Python virtual environment.
python3 -m venv venv
source venv/bin/activatepython -m venv venv
venv\Scripts\activateThis will create and activate a virtual environment in a folder named venv.
Once your virtual environment is active, install the necessary dependencies from requirements.txt:
pip install -r requirements.txtThis will install all the required packages listed in the requirements.txt file.
To securely store sensitive information like CLIENT_ID and CLIENT_SECRET, use a .env file.
Create a file named .env in the root directory of your project and add the following content:
CLIENT_ID=your-client-id
CLIENT_SECRET=your-client-secret
Replace your-client-id and your-client-secret with the actual values provided by Sayari.
Once you're done with your development, you can deactivate the virtual environment:
deactivatefrom sayari.client import Sayari
client = Sayari(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)from sayari.client import AsyncSayari
client = AsyncSayari(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)In your Python code, ensure you load the environment variables using python-dotenv:
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Access the variables
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
if client_id is None or client_secret is None:
raise ValueError("CLIENT_ID and CLIENT_SECRET must be set in the .env file or as environment variables.")Once authenticated, you can use the client object to interact with the Sayari API. The client is returned by the load_env_vars_and_authenticate function, which handles all environment setup and authentication.
client = load_env_vars_and_authenticate()
# Now, you can use 'client' to make API calls.-
Missing CLIENT_ID or CLIENT_SECRET: Ensure that your
.envfile contains bothCLIENT_IDandCLIENT_SECRET. If not present, the script will exit with an error message. -
Python Version Error: If the script exits with the message
"Python 3.9.x or higher is required", make sure you are running the correct version of Python.
The Sayari client includes automatic OAuth access token retrieval, refreshing it as needed. Simply specify the client_id
and client_secret (shown above) and your access token will be used for every authenticated request.
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version. This way, you can install the same version each time without breaking changes.