# The HTTP client

Canonical URL: https://developers.iagenify.com/sdk/concepts/client/
Language: en

One client underneath every resource. It attaches the session, carries the machine headers, parses the answer, and turns a failure into a typed error before you ever see it.

Status: one instance per IaGenify · typed failures

You rarely touch it directly — `api.models`, `api.storage` and the rest all go through it. Knowing what it does is still worth a page, because its behaviour is what you are debugging when a call does something unexpected.

## What happens on every call {#lifecycle}

| Step | What it does |
| --- | --- |
| 1 · Session | Gets a valid JWT, refreshing or exchanging the key if needed. Skipped for the few calls that need no identity. |
| 2 · Headers | Adds the version, the machine id and the detected resources. |
| 3 · Body | JSON, unless you passed FormData — then the Content-Type is left to the runtime. |
| 4 · Timer | Aborts after the timeout, unless the timeout is 0. |
| 5 · Parse | JSON when it can; otherwise the raw text, as { msg }. |
| 6 · Failure | One retry on 401, then a typed error carrying status, code and details. |

## A response that is not JSON {#parsing}

The client never throws on a body it cannot parse. A non-JSON success comes back as `{ msg: "…" }` instead, which is what you will see if something between you and the API answers in its place — a proxy, a captive portal, an error page from a gateway.

```javascript
const result = await assistant.generate({ inputs });

if (result.msg !== undefined && result.outputs === undefined) {
  // Something answered, but it was not the API
  console.error("unexpected body:", result.msg.slice(0, 200));
}
```

## Uploads {#formdata}

When a body is `FormData`, the client does not set `Content-Type`. That is deliberate: the boundary is generated by the runtime, and setting the header by hand produces a body the server cannot split.

## Pointing it somewhere else {#base}

```javascript
// Explicit
const api = new IaGenify({ apiKey, baseUrl: "https://api.iagenify.com" });

// From the environment
// IAGENIFY_API_URL=https://api.iagenify.com
const api = new IaGenify({ apiKey });

api.baseUrl;   // whichever won
```

With neither, the client has no address to call and every request fails with `unreachable`. In a deployed service, `baseUrl` is the first thing to check: read `api.baseUrl` and confirm it is `https://api.iagenify.com`.

- [Errors](/sdk/concepts/errors): The three classes, and the fields worth branching on.
- [Timeouts and abort](/sdk/concepts/timeouts): Which calls have a deadline and which do not.

```python
result = assistant.generate(inputs=inputs)

if "msg" in result and "outputs" not in result:
    # Something answered, but it was not the API
    print("unexpected body:", str(result["msg"])[:200])
```

```python
# Explicit
api = IaGenify(api_key=api_key, base_url="https://api.iagenify.com")

# From the environment
# IAGENIFY_API_URL=https://api.iagenify.com
api = IaGenify(api_key=api_key)

api.base_url   # whichever won
```
