> For the complete documentation index, see [llms.txt](https://docs.envector.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.envector.io/1.4.x/sdk-user-guide/initialize/connecting-to-the-service.md).

# Connection

This guide explains how to establish and verify the connection to the `enVector` service using the Python client SDK.

***

### Initializing the Connection

The first step to communicating with the enVector service is to establish a connection. This configuration tells the SDK the network location of the running service and provides any necessary authentication credentials.

#### **Key Parameters**

* `host` (str): Specifies the hostname (e.g., `api.my-company.com`) or IP address (e.g., `192.168.1.10`) of the server.
* `port` (int): The network port number on which the server is listening for connections (e.g., `50050`).
* `address` (str): A convenience parameter to specify both the `host` and `port` in a single `"host:port"` formatted string (e.g., `"localhost:50050"`).

  > Note: If the `address` parameter is used, you do not need to provide `host` and `port` separately. Using `address` is generally simpler and recommended.
* `access_token` (str, Optional): The access token used for authentication if the server requires it for security. When provided, the SDK will include this token in all requests to authenticate itself.

#### Example

```python
import pyenvector as ev

# Method 1: Connecting with the address parameter (Recommended)
# This is the most concise and common method.
ev.init_connect(
    address="localhost:50050",
    access_token="your-secret-token-if-needed"
)

# Method 2: Connecting by specifying host and port separately
ev.init_connect(
    host="localhost",
    port=50050,
    access_token="your-secret-token-if-needed"
)
```

***

### Access Tokens

If your server is configured with authentication or you are using enVector SaaS, you must provide an `access_token`:

```python
import os
import pyenvector as ev

token = os.environ.get("ENVECTOR_ACCESS_TOKEN")  # or your configured token
ev.init_connect(address="api.envector.example:50050", access_token=token)
```

Notes:

* Omit `access_token` or pass `None` only when your local/server deployment does not require authentication.
* Store tokens securely (e.g., environment variables, secret managers), and never commit them to source control.

***

### Checking the Connection Status

After running the initialization, you can verify whether the connection to the `enVector` service was successfully established. The `ev.is_connected()` function is used for this purpose.

This function returns a boolean value:

* `True` if the connection is active and ready.
* `False` if the connection has not been established or has been lost.

#### Example

You can use this function to confirm the connection status before making other calls.

```python
import pyenvector as ev

# Initialize the connection
ev.init_connect(address="localhost:50050", access_token=None)

# Check if the connection is successful
if ev.is_connected():
    print("Successfully connected to ev! ✅")
else:
    print("Failed to connect to ev. ❌")

# Returns: True
is_active = ev.is_connected()
print(f"Connection status: {is_active}")
```

***

### ⏏️ Disconnecting from the Service

You can explicitly terminate the connection to the `enVector` service using the `ev.disconnect()` function.

Calling this function will close the current session. After disconnecting, you must call `ev.init_connect()` again to establish a new connection before performing any other operations.

#### Example

This example shows the full cycle of connecting, checking the status, disconnecting, and then checking the status again.

```python
import pyenvector as ev

# 1. Initialize the connection
ev.init_connect(address="localhost:50050", access_token=None)

# 2. Check that the connection is active
if ev.is_connected():
    print(f"Connection active: {ev.is_connected()} ✅")
else:
    print("Failed to connect.")

# 3. Disconnect from the service
print("Disconnecting from the service...")
ev.disconnect()

# 4. Check the connection status again
if not ev.is_connected():
    print(f"Connection active: {ev.is_connected()} ❌")
else:
    print("Still connected.")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.envector.io/1.4.x/sdk-user-guide/initialize/connecting-to-the-service.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
