> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript SDK: analytics, DNS and domains

> Read edge analytics, errors, request logs and performance of a web application, check DNS, set a custom domain and purge the cache with api.apps.network.

`api.apps.network` works on **web apps only**. Every method takes the app id first.

Examples use the `api` client from [Creating the client](/en/sdks/js/client#creating-the-client). `appId` is the id of one of your apps: [`api.account.me()`](/en/sdks/js/client#account) lists them.

## Time windows

`analytics`, `errors`, `logs` and `performance` take a `start` and an `end`, each an ISO 8601 string or a `Date`. A window is at most **7 days**, and `start` is clamped to the app's creation date. An invalid window is 400 `INVALID_TIME_RANGE`.

```typescript theme={"system"}
const end = new Date();
const start = new Date(end.getTime() - 24 * 60 * 60 * 1000); // last 24 h
```

Results are cached in 5-minute buckets. Cache misses of these four methods share a limit of 20 requests per 60 seconds per account: going over it is 429 `RATE_LIMITED`.

## Analytics

`network.analytics(appId, start, end, filters?)` returns traffic totals and breakdowns. It returns `null` for a window with no traffic.

```typescript theme={"system"}
const analytics = await api.apps.network.analytics(appId, start, end);

if (analytics) {
    console.log(analytics.visits);    // [{ visits, requests, bytes, date }]
    console.log(analytics.countries); // [{ type: "BR", visits, requests, bytes }]
}
```

The breakdowns are `countries`, `devices`, `os`, `browsers`, `protocols`, `methods`, `paths`, `referers`, `providers`, `ips`, `status_codes`, `bots` and `content_types`. Each bucket has `type`, `visits`, `requests` and `bytes`.

### Filters

Filters narrow every breakdown. Each takes the exact `type` value of its breakdown:

```typescript theme={"system"}
const brazil = await api.apps.network.analytics(appId, start, end, {
    country: "BR",
    path: "/api",
    provider: "GOOGLE (15169)",
});
```

| Filter | Example |
| - | - |
| `country` | `"BR"` (2-letter code) |
| `ip` | `"203.0.113.7"` |
| `path` | `"/api"` (path prefix) |
| `status` | `"404"` |
| `os`, `browser`, `protocol`, `bot` | The breakdown's `type` |
| `referer` | `"Direct"` means no referer |
| `provider` | `"NAME (ASN)"`, e.g. `"GOOGLE (15169)"` |
| `content_type` | The breakdown's `type` |

An invalid filter is 400 `INVALID_FILTER`.

## Errors

`network.errors(appId, start, end, { include_4xx })` returns the 5xx responses of the window, plus 4xx with `include_4xx: true`. It returns `null` for an empty window.

```typescript theme={"system"}
const errors = await api.apps.network.errors(appId, start, end, { include_4xx: true });

if (errors) {
    console.log(errors.summary.total, errors.summary.by_class);
    console.log(errors.top_paths);
}
```

## Request logs

`network.logs(appId, start, end)` returns individual requests with client, request and response details. Available on Pro and Enterprise plans (403 `UPGRADE_REQUIRED` otherwise).

```typescript theme={"system"}
for (const log of await api.apps.network.logs(appId, start, end)) {
    console.log(log.timestamp, log.request.method, log.request.path, log.response.status);
}
```

## Performance

`network.performance(appId, start, end)` returns latency percentiles (`p50`, `p95`, `p99`, in ms) at the edge and at the origin, by country, by location and for the slowest paths. It returns `null` for an empty window. Available on Pro and Enterprise plans.

```typescript theme={"system"}
const performance = await api.apps.network.performance(appId, start, end);

if (performance) {
    console.log(performance.summary.edge.p95, performance.summary.origin.p95);
}
```

## DNS

`network.dns(appId)` returns the DNS records your custom domain needs, with their status. Without a custom domain it is 400 `NO_CUSTOM_DOMAIN`.

```typescript theme={"system"}
for (const { type, name, value, status } of await api.apps.network.dns(appId)) {
    console.log(type, name, value, status); // type: "txt" | "cname"
}
```

## Custom domain

`network.setDomain(appId, domain)` attaches a custom domain (Standard plan and up). Pass `"@"` to remove it.

```typescript theme={"system"}
await api.apps.network.setDomain(appId, "www.example.com");

// Remove it
await api.apps.network.setDomain(appId, "@");
```

| Status | Code | When |
| - | - | - |
| 400 | `INVALID_DOMAIN` | Not a valid domain name |
| 400 | `RESERVED_DOMAIN` | A Square Cloud domain |
| 403 | `UPGRADE_REQUIRED` | The plan has no custom domains |
| 403 | `LOAD_BALANCER_LIMIT_REACHED` | The plan's limit of apps on this domain was reached |
| 409 | `DOMAIN_ALREADY_EXISTS` | The domain belongs to an app of another account |
| 502 | `DNS_FAILED` | The edge provider refused the domain |

## Purging the cache

`network.purgeCache(appId)` clears the app's edge cache. It is limited to one purge every 60 seconds (429 `KEEP_CALM`).

```typescript theme={"system"}
await api.apps.network.purgeCache(appId);
```

## Errors of the analytics methods

| Status | Code | When |
| - | - | - |
| 400 | `INVALID_TIME_RANGE` | `start`/`end` missing, malformed or inverted |
| 400 | `INVALID_FILTER` | A filter does not match its format (`analytics`) |
| 403 | `UPGRADE_REQUIRED` | `logs` and `performance` outside Pro and Enterprise |
| 429 | `RATE_LIMITED` | Shared limit of cache misses reached |
| 500 | `UNABLE_TO_FETCH_ANALYTICS`, `UNABLE_TO_FETCH_ERRORS`, `UNABLE_TO_FETCH_PERFORMANCE` | The edge provider failed |
| 503 | `ANALYTICS_BUSY` | Edge analytics are busy: retried automatically |

## Next steps

<CardGroup cols={3}>
  <Card title="Realtime" icon="wave-pulse" href="/en/sdks/js/realtime">
    Stream live logs and status.
  </Card>

  <Card title="Network API reference" icon="code" href="/en/api-reference/endpoint/apps/network/analytics">
    The REST endpoints behind these methods.
  </Card>

  <Card title="Edge analytics from the CLI" icon="terminal" href="/en/cli-reference/edge-analytics">
    The same actions from the terminal.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.