# Client

> Connection settings for the platform API.

## noclick.Client

```python
class Client
```

Connection settings for the platform API.

Most code never builds one: the module-level default reads
`NOCLICK_API_KEY` and `NOCLICK_API_URL` (inside a deployed app without
a key, its deployment's own credential, `NOCLICK_DEPLOYMENT_TOKEN`). Pass
`client=` to any constructor or classmethod to use another account or
endpoint.
`api_key` may be a function (sync or async) returning the bearer; it is
called on every attempt, so a refreshing token stays current.

Usage:

Use a second account by passing its client as `client=`:

```python
other = noclick.Client(api_key="nk_live_other")
users = noclick.User.list(client=other)
```

Tune retries and timeouts, and close the connections when the block ends:

```python
with noclick.Client(api_key="nk_live_other", timeout=30, max_retries=5) as other:
    print(noclick.User.list(client=other, limit=10))
```

When your key comes from a secret store that rotates it, pass the function that reads it:

```python
client = noclick.Client(api_key=read_noclick_key)   # called before every attempt
```

| Attribute | Type | Description |
| --- | --- | --- |
| `base_url` |  |  |
| `timeout` | `float` |  |
| `max_retries` | `int` |  |
| `backoff` | `float` |  |
| `max_backoff` | `float` |  |
| `api_key` | `ApiKey | None` |  |
| `in_app` | `bool` | Whether this client signs in as a deployed app's own code (its deployment's credential), where `from_name` resolves names in the app's own namespace for the environment it runs in. |

### noclick.Client

```python
noclick.Client(
    api_key=None, base_url=None, *, timeout=60.0, max_retries=3, backoff=0.5,
    max_backoff=8.0, headers=None
)
```

Parameters:

- `api_key` (`ApiKey | None`, default `None`)
- `base_url` (`str | None`, default `None`)
- `timeout` (`float`, default `60.0`)
- `max_retries` (`int`, default `3`)
- `backoff` (`float`, default `0.5`)
- `max_backoff` (`float`, default `8.0`)
- `headers` (`dict[str, str] | None`, default `None`)

### Client.for_session

```python
noclick.Client.for_session(session, *, base_url=None, **kwargs) -> Client
```

Build a client that authenticates with a user session (a `noclick.Session`
or its `nk_sess_...` token). Every call acts for the session's user
with the session's scopes; the same as `noclick.Client(api_key=token)`.

Parameters:

- `session` (`Any`, required)
- `base_url` (`str | None`, default `None`)
- `kwargs` (`Any`)

Returns: `Client` A client for that session, on the session's own API endpoint unless you pass `base_url`.

Usage:

```python
user = noclick.User.from_id("user_42")
session = user.session(["agents"], ttl_s=3600)
as_ana = noclick.Client.for_session(session)
agents = noclick.Agent.list(client=as_ana)   # Ana's agents and the shared ones, nothing else
```

### Client.aclose

```python
client.aclose() -> None
```

Close the connection pool opened on the current event loop.

Usage:

In a long-lived async app, close the client when you shut down:

```python
await noclick.default_client().aclose()
```

### Client.close

```python
client.close() -> None
```

Close the pool the blocking API uses.

Usage:

```python
client = noclick.Client(api_key="nk_live_other")
users = noclick.User.list(client=client, limit=10)
client.close()
```

### Client.request

```python
client.request(
    method, path, *, json=None, content=None, params=None, user=...,
    idempotency_key=None, timeout=None, binary=False
) -> Any
```

Send one API call. `content` sends raw bytes instead of JSON.

Writes carry an `Idempotency-Key` (generated unless given) that is
reused on every retry, so a retried POST can't do the work twice.

Parameters:

- `method` (`str`, required)
- `path` (`str`, required)
- `json` (`Any`, default `None`)
- `content` (`bytes | None`, default `None`)
- `params` (`dict[str, Any] | None`, default `None`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `idempotency_key` (`str | None`, default `None`): Makes a retried create safe: the same key returns the first result. Generated when omitted.
- `timeout` (`float | None`, default `None`)
- `binary` (`bool`, default `False`)

Returns: `Any` The response's decoded JSON (`None` for an empty body), or its raw bytes with `binary=True`.
An error response raises its `noclick.Error` instead.

Usage:

Call an endpoint directly, with the client's key, retries and errors. It is async only:

```python
client = noclick.default_client()
page = await client.request("GET", "/v1/users", params={"limit": 5}, user=None)
print([row["id"] for row in page["data"]])
```

Pass your own `idempotency_key` to make a write safe to repeat across processes:

```python
client = noclick.default_client()
user = await client.request(
    "POST", "/v1/users", json={"id": "user_42"}, user=None, idempotency_key="signup-user_42",
)
```

### Client.console_url

```python
client.console_url(section, object_id) -> str | None
```

Link an object (in `operations`, `agents` or `users`) to its
developer console page.

Parameters:

- `section` (`str`, required)
- `object_id` (`str`, required)

Returns: `str | None` The page's URL, or `None` when this client doesn't talk to the hosted API.

Usage:

Operations, agents and users have a `console_url` of their own. Build one from an id you stored:

```python
url = noclick.default_client().console_url("operations", "3b9e0f6a-1c2d-4e5f-8a9b-0c1d2e3f4a5b")
```

## noclick.configure

```python
noclick.configure(api_key=None, base_url=None, **kwargs) -> Client
```

Replace the default client, e.g. `noclick.configure(api_key="nk_live_...")`.

Parameters:

- `api_key` (`ApiKey | None`, default `None`)
- `base_url` (`str | None`, default `None`)
- `kwargs` (`Any`)

Returns: `Client` The new default client, which every call without `client=` now uses.

Usage:

```python
noclick.configure(api_key="nk_live_...", timeout=30, max_retries=5)
users = noclick.User.list(limit=10)   # uses the new default
```

## noclick.default_client

```python
noclick.default_client() -> Client
```

Get the client used when no `client=` is given, built from the environment on first use.

Returns: `Client` The shared default client: the one `configure` last set, else one built from `NOCLICK_API_KEY`
and `NOCLICK_API_URL` (inside a deployed app without a key, signed in as the app with
`NOCLICK_DEPLOYMENT_TOKEN`).

Usage:

```python
client = noclick.default_client()
print(client.base_url)   # https://api.noclick.com unless NOCLICK_API_URL says otherwise
```

## noclick.__version__

```python
noclick.__version__ = '0.2.2'
```

This client's version.
## noclick.DEFAULT_BASE_URL

```python
noclick.DEFAULT_BASE_URL = 'https://api.noclick.com'
```

The hosted API; `NOCLICK_API_URL` (or `base_url=`) points a client elsewhere.
## noclick.CONSOLE_BASE_URL

```python
noclick.CONSOLE_BASE_URL = 'https://noclick.com/dashboard/developers'
```

Where the hosted API's objects are shown (only for the hosted base URL).
