Skip to main content

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.

# Set these environment variables
# WEAVIATE_URL your Weaviate instance URL
# WEAVIATE_GPC_URL your Weaviate GPC connection URL
# WEAVIATE_API_KEY your Weaviate instance API key

import weaviate, os
from weaviate.auth import AuthApiKey

client = weaviate.connect_to_custom(
http_host=os.getenv("WEAVIATE_URL"), # URL only, no http prefix
http_port=443,
http_secure=True, # Set to True if https
grpc_host=os.getenv("WEAVIATE_GPC_URL"),
grpc_port=443, # Default is 50051, WCD uses 443
grpc_secure=True, # Edit as needed
auth_credentials=AuthApiKey(os.getenv("WEAVIATE_API_KEY")),
)

print(client.is_ready())

Environment variables

danger

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.
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.

# Set these environment variables
# WEAVIATE_URL your Weaviate instance URL
# WEAVIATE_GPC_URL your Weaviate GPC connection URL
# WEAVIATE_API_KEY your Weaviate instance API key

import weaviate, os
from weaviate.auth import AuthApiKey
from weaviate.classes.init import AdditionalConfig, Timeout

client = weaviate.connect_to_custom(
http_host=os.getenv("WEAVIATE_URL"), # URL only, no http prefix
http_port=443,
http_secure=True, # Set to True if https
grpc_host=os.getenv("WEAVIATE_GPC_URL"),
grpc_port=443, # Default is 50051, WCD uses 443
grpc_secure=True, # Edit as needed
auth_credentials=AuthApiKey(os.getenv("WEAVIATE_API_KEY")),
additional_config=AdditionalConfig(
timeout=Timeout(init=30, query=60, insert=120) # Values in seconds
)
)

print(client.is_ready())

OIDC authentication

For details on authentication with OpenID Connect (OIDC), see OIDC configuration.

# Set these environment variables
# WEAVIATE_USER your Weaviate OIDC username
# WEAVIATE_PWD your Weaviate OIDC password
# WEAVIATE_URL your Weaviate instance URL

import os
import weaviate

client = weaviate.connect_to_weaviate_cloud(
cluster_url=os.getenv("WEAVIATE_URL"), # Replace with your Weaviate Cloud URL
auth_credentials=weaviate.auth.AuthClientPassword(
username=os.getenv("WEAVIATE_USER"), # Your Weaviate Cloud username
password=os.getenv("WEAVIATE_PWD") # Your Weaviate Cloud password
)
)

Questions and feedback

If you have any questions or feedback, let us know in the user forum.