Connect with the Python client
Learn how to connect to a Serverless (Vector) database with the AstraPy client.
Prerequisites
-
You have an active Astra account
-
You have created a Serverless (Vector) database.
-
You have generated an application token with the Database Administrator role.
-
You have installed Python 3.8 or higher, and pip 23.0 or higher.
Install the Python client
Use pip to install the Python client.
To install the Python client with pip:
-
Verify that pip is version 23.0 or higher.
pip --version
-
Upgrade pip if needed.
python -m pip install --upgrade pip
-
Install the
astrapy
package. You must have Python 3.8 or higher.pip install astrapy
Connect to a vector-enabled Astra DB Serverless database
Create a file named quickstart.py
with the following content:
import os
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.ids import UUID
from astrapy.exceptions import InsertManyException
# Initialize the client and get a "Database" object
client = DataAPIClient(os.environ["ASTRA_DB_APPLICATION_TOKEN"])
database = client.get_database_by_api_endpoint(os.environ["ASTRA_DB_API_ENDPOINT"])
print(f"* Database: {database.info().name}\n")
Run the script with python quickstart.py
.
Next steps
-
See a complete Python example of how to connect to a database, load data into a collection, and perform a similarity search.
-
Read the Python client reference for more details about available methods.
-
Also see the Python tab for details and examples in each task-based section of the API Reference topics: