Custom connections
The Python Client v4 and the TypeScript Client v3 provide helper methods for common connection types. They also provide custom methods for when you need additional connection configuration.
If you are using one of the other clients, the standard connection methods are configurable for all connections.
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.config import AdditionalConfig, Timeout
# Best practice: store your credentials in environment variables
http_host = os.environ["WEAVIATE_HTTP_HOST"]
grpc_host = os.environ["WEAVIATE_GRPC_HOST"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
client = weaviate.connect_to_custom(
http_host=http_host, # Hostname for the HTTP API connection
http_port=443, # Default is 80, WCD uses 443
http_secure=True, # Whether to use https (secure) for the HTTP API connection
grpc_host=grpc_host, # Hostname for the gRPC API connection
grpc_port=443, # Default is 50051, WCD uses 443
grpc_secure=True, # Whether to use a secure channel for the gRPC API connection
auth_credentials=Auth.api_key(weaviate_api_key), # API key for authentication
)
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.config import AdditionalConfig, Timeout
# Best practice: store your credentials in environment variables
http_host = os.environ["WEAVIATE_HTTP_HOST"]
grpc_host = os.environ["WEAVIATE_GRPC_HOST"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
client = weaviate.connect_to_custom(
http_host=http_host, # Hostname for the HTTP API connection
http_port=443, # Default is 80, WCD uses 443
http_secure=True, # Whether to use https (secure) for the HTTP API connection
grpc_host=grpc_host, # Hostname for the gRPC API connection
grpc_port=443, # Default is 50051, WCD uses 443
grpc_secure=True, # Whether to use a secure channel for the gRPC API connection
auth_credentials=Auth.api_key(weaviate_api_key), # API key for authentication
additional_config=AdditionalConfig(
timeout=Timeout(init=30, query=60, insert=120) # Values in seconds
)
)
print(client.is_ready())
Third party API keys
Integrations that use external APIs often need API keys. 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 weaviate, os
from weaviate.classes.init import Auth
from weaviate.config import AdditionalConfig, Timeout
# Best practice: store your credentials in environment variables
http_host = os.environ["WEAVIATE_HTTP_HOST"]
grpc_host = os.environ["WEAVIATE_GRPC_HOST"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
cohere_api_key = os.environ["COHERE_API_KEY"]
client = weaviate.connect_to_custom(
http_host=http_host, # Hostname for the HTTP API connection
http_port=443, # Default is 80, WCD uses 443
http_secure=True, # Whether to use https (secure) for the HTTP API connection
grpc_host=grpc_host, # Hostname for the gRPC API connection
grpc_port=443, # Default is 50051, WCD uses 443
grpc_secure=True, # Whether to use a secure channel for the gRPC API connection
auth_credentials=Auth.api_key(weaviate_api_key), # API key for authentication
headers={"X-Cohere-Api-Key": cohere_api_key}, # Third party API key (e.g. Cohere)
)
print(client.is_ready())
OIDC authentication
OIDC lets you authenticate to a self-hosted Weaviate instance using a bearer access token issued by your identity provider (Keycloak, Okta, Azure AD, Auth0, etc.). Your application obtains the token from the IdP, then passes it to the Weaviate client and the client attaches it to every request.
For server-side configuration (enabling OIDC on Weaviate), the supported authentication flows (client credentials, resource owner password, hybrid), and how to obtain tokens from your IdP, see the OIDC configuration guide.
Connecting to Weaviate Cloud (WCD) using OIDC is deprecated and should not be used. Please use API key authentication instead.
The examples below assume you already have a bearer access token. Set the following environment variables before running them:
WEAVIATE_HTTP_HOST— host:port of the Weaviate REST endpoint (e.g.,localhost:8080)WEAVIATE_GRPC_HOST— host:port of the Weaviate gRPC endpoint (e.g.,localhost:50051)WEAVIATE_OIDC_ACCESS_TOKEN— the access token from your IdPWEAVIATE_OIDC_REFRESH_TOKEN— (optional) refresh token for automatic renewalWEAVIATE_OIDC_EXPIRES_IN— (optional) token lifetime in seconds
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
# Connect to a self-hosted Weaviate instance configured with OIDC.
# Obtain the access token from your identity provider before connecting.
client = weaviate.connect_to_custom(
http_host="localhost",
http_port=8580,
http_secure=False,
grpc_host="localhost",
grpc_port=50551,
grpc_secure=False,
auth_credentials=Auth.bearer_token(
access_token=os.environ["WEAVIATE_OIDC_ACCESS_TOKEN"],
refresh_token=os.environ.get("WEAVIATE_OIDC_REFRESH_TOKEN"),
expires_in=int(os.environ.get("WEAVIATE_OIDC_EXPIRES_IN", "60")),
),
)
Questions and feedback
If you have any questions or feedback, let us know in the user forum.
