Weaviate Cloud
Follow these steps to connect to a Weaviate Cloud (WCD) instance.
Retrieve your API key and REST endpoint
Open the Weaviate Cloud console and follow the steps below:
Connection example
To connect, use the REST Endpoint and the Admin API key stored as environment variables:
If a snippet doesn't work or you have feedback, please open a GitHub issue.
import weaviate
from weaviate.classes.init import Auth
# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
# Connect to Weaviate Cloud
client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url,
auth_credentials=Auth.api_key(weaviate_api_key),
)
print(client.is_ready()) # Should print: `True`
client.close() # Free up resources
Third party API keys
If you use API-based models for vectorization or RAG, you must provide an API key for the service. To add third party API keys, follow these examples:
If a snippet doesn't work or you have feedback, please open a GitHub issue.
import os
import weaviate
from weaviate.classes.init import Auth
# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
cohere_api_key = os.environ["COHERE_API_KEY"]
# Connect to Weaviate Cloud
client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url,
auth_credentials=Auth.api_key(weaviate_api_key),
headers={
"X-Cohere-Api-Key": cohere_api_key
}
)
print(client.is_ready())
Environment variables
Do not hard-code API keys or other credentials in your client code. Use environment variables or a similar secure coding technique instead.
Environment variables keep sensitive details out of your source code. Your application imports the information to runtime.
Set an environment variable.
In these examples, the environment variable names are in UPPER_CASE.
export WEAVIATE_URL="http://localhost:8080"
export WEAVIATE_API_KEY="sAmPleKEY8FwELJILn0YDRG9gjy4hReqfInz"
Import an environment variable.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
weaviate_url = os.getenv("WEAVIATE_URL")
weaviate_key = os.getenv("WEAVIATE_API_KEY")
gRPC timeouts
The Python client v4 and TypeScript client v3 use gRPC. The gRPC protocol is sensitive to network delay. If you encounter connection timeouts, adjust the timeout values for initialization, queries, and insertions.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
import weaviate, os
from weaviate.classes.init import Auth
from weaviate.classes.init import AdditionalConfig, Timeout
# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
# Connect to a WCD instance
client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url,
auth_credentials=Auth.api_key(weaviate_api_key),
# skip_init_checks=True,
additional_config=AdditionalConfig(
timeout=Timeout(init=30, query=60, insert=120) # Values in seconds
)
)
print(client.is_ready())
Questions and feedback
If you have any questions or feedback, let us know in the user forum.
