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.

import weaviate, os
from weaviate.classes.init import Auth

# Best practice: store your credentials in environment variables
http_host = os.environ["WCD_HTTP_HOST"]
grpc_host = os.environ["WCD_GRPC_HOST"]
weaviate_api_key = os.environ["WCD_DEMO_RO_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

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.

import weaviate, os
from weaviate.classes.init import Auth

# Best practice: store your credentials in environment variables
http_host = os.environ["WCD_HTTP_HOST"]
grpc_host = os.environ["WCD_GRPC_HOST"]
weaviate_api_key = os.environ["WCD_DEMO_RO_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())

OIDC authentication

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

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_username = os.environ["WCD_USERNAME"]
weaviate_password = os.environ["WCD_PASSWORD"]

client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.client_password(
username=weaviate_username, # Your Weaviate Cloud username
password=weaviate_password # Your Weaviate Cloud password
)
)

Questions and feedback

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