# @connect

## Overview

The `@connect` decorator is used to wrap functions that handle [**connections**](https://docs.kognitos.com/legacy/legacy-experience/books/custom-books/api-reference/connections) or authentication tasks. It defines the syntax for writing a [connection command](https://docs.kognitos.com/legacy/legacy-experience/books/connections), 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**](https://docs.kognitos.com/legacy/legacy-experience/books/custom-books/api-reference/connections) for additional context and usage examples.
