Connect with the Python client
You can connect to your Serverless (Vector) databases with the astrapy client.
For a guided experience to get started with the Python client, see the Quickstart. |
Prerequisites
-
An active Astra account
-
An active Serverless (Vector) database
Install the Python client
Install the Python client with pip:
-
Verify that pip is version 23.0 or later:
pip --version
-
If needed, upgrade pip:
python -m pip install --upgrade pip
-
Install the astrapy package . You must have Python 3.8 or later.
pip install astrapy
Upgrade the Python client
When a new client version is released, upgrade your client to get the latest features, improvements, and bug fixes. For information about major changes in specific client versions, see Compare Data API client versions.
# verbose
pip install astrapy --upgrade
# shorthand
pip install -U astrapy
Set environment variables
The Data API requires an application token and your database’s API endpoint for most operations. Store these values in environment variables to simplify reuse in your scripts:
-
Generate an application token with the Database Administrator role, and then get your database’s API endpoint in the form of
https://DATABASE_ID-REGION.apps.astra.datastax.com
. For more information, see Generate an application token for a database. -
Set environment variables for your token and API endpoint:
-
Linux or macOS
-
Windows
export ASTRA_DB_API_ENDPOINT=API_ENDPOINT export ASTRA_DB_APPLICATION_TOKEN=TOKEN
set ASTRA_DB_API_ENDPOINT=API_ENDPOINT
set ASTRA_DB_APPLICATION_TOKEN=TOKEN
-
Connect to a Serverless (Vector) database
When you use a Data API client, your main entry point is the DataAPIClient
object.
Then, you get a database
object to connect to a database.
These operations are the basis of your client scripts.
For more information, see Instantiate a client object and Connect to a database.
import os
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
# Initialize the client and get a "Database" object
client = DataAPIClient(os.environ["ASTRA_DB_APPLICATION_TOKEN"])
database = client.get_database(os.environ["ASTRA_DB_API_ENDPOINT"])
print(f"* Database: {database.info().name}\n")
Next steps
After you connect to a database, you can extend your script to work with the collections and documents in it. You can also use the Python client to manage databases and keyspaces.
For more information about Astra DB APIs, see Intro to Astra DB APIs and Get started with the Data API.