# @connect

## Overview

The `@connect` decorator is used to wrap functions that handle [**connections**](/legacy/legacy-experience/books/custom-books/api-reference/connections.md) or authentication tasks. It defines the syntax for writing a [connection command](/legacy/legacy-experience/books/connections.md), which instantiates the connection in Kognitos to the third-party API or service that's implemented in your Python function.

## Syntax

```python
@connect(*args, **kwargs)
```

## Keyword Arguments

<table><thead><tr><th width="155.8359375">Argument</th><th width="86.75390625">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>str</code></td><td>A name to associate with the connection. If not provided, it defaults to the function name.</td></tr><tr><td><code>noun_phrase</code></td><td><code>str</code></td><td><p>A unique label to identify the authentication method. If not provided, it will be inferred from the function name. Examples include, but are not limited to:</p><ul><li><code>noun_phrase="api keys"</code></li><li><code>noun_phrase="client credentials"</code></li><li><code>noun_phrase="user password authentication"</code></li></ul></td></tr></tbody></table>

## Implementation Example

This is an example implementation of connecting to the OpenWeather API using an API key:

```python
@connect(noun_phrase="api keys")
def connect(self, api_key: str):
  """
  Authenticate to Open Weather API using the specified API key. 
  You can obtain your own API key by visiting Open Weather's 
  website at https://openweathermap.org/appid.

  Arguments:
      api_key: The API key to be used for connecting

  Labels:
      api_key: API Key
  """
  api_key = os.getenv("API_KEY", api_key)
  test_url = f"{self._base_url}?appid={api_key}&q=London"
  response = requests.get(test_url, timeout=self._timeout)
  if response.status_code == 401:
      response_data = response.json()
      if "Invalid API key" in response_data.get("message", ""):
          raise ValueError("Invalid API key")

  self._api_key = api_key
```

Refer to [**connections**](/legacy/legacy-experience/books/custom-books/api-reference/connections.md) for additional context and usage examples.


---

# Agent Instructions: 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:

```
GET https://docs.kognitos.com/legacy/legacy-experience/books/custom-books/api-reference/decorators/connect-decorator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
