# App fetch `appFetch` sends an HTTP request to a hosted package app as the signed-in person, and follows the app's own redirects unless you ask it not to. Use [`appList`](#list-apps) to find the address, then [call it](#call-shape). For a walkthrough, see [Using apps as an agent](../guides/agent-apps.md). **An app request can have real side effects**, including a synthetic smoke test. ## List apps `appList` lists hosted apps that pass the caller's `app:execute` check in the owning org. It searches orgs the person belongs to or has a grant in, not the entire public catalog. Filter to an org with `{ "owner": "acme" }`. Results include `app` (the `@org/app` address), `package_id`, `owner`, `hosted_url`, and `visibility`. `shared: true` means the person has no membership role in the owning org. The list sorts by app address and returns 100 per page by default (maximum 200). Pass `next_cursor` as `cursor` for the next page; `null` marks the last page. ## Who may call it - Interactive MCP, `execute`, the Open API (`POST /v1/apps/fetch`), and the CLI (`kody app fetch`). - The caller needs `app:execute` on that app in the org that owns it. A profile token also needs the grant on every layer of its profile chain, and the token's scopes must include `app:execute`. - Code running inside a package app, a job, or a trigger cannot call it. ## Call shape ```json { "app": "@acme/board" } ``` | Field | Default | Meaning | | ------------ | ------------------ | --------------------------------------------------------------------- | | `app` | | `@org/app`, or the app's hosted URL. `appList` returns it | | `package_id` | | The app's package id, instead of `app` | | `path` | App address or `/` | Path **after** the app mount (what the handler sees) | | `method` | `GET` | HTTP method | | `headers` | `{}` | Extra request headers (safe subset) | | `body` | omitted | Raw request body string for `POST` / `PUT` / `PATCH`, up to 100 KB | | `synthetic` | `false` | `true` marks a smoke test with the `Kody-Synthetic: true` header | | `redirect` | `follow` | `manual` returns the first response, so a 3xx keeps its `Location` | | `max_bytes` | 102400 | Most response bytes to return. Up to 10485760 on the Open API and CLI | Pass `app` or `package_id`. The lookup uses the org the address names, not the org your connection is bound to, which is what lets a shared app in another org work. If you omit `path`, the call uses the path in the app address if present, or `/` otherwise. Example POST with JSON: ```json { "app": "@acme/board", "path": "/api/items", "method": "POST", "headers": { "content-type": "application/json" }, "body": "{\"name\":\"smoke\"}" } ``` From the CLI: ```sh kody app fetch @acme/board /api/items --method POST \ --header "content-type: application/json" --body @item.json kody app fetch https://board--acme.kody.run/export.csv --out export.csv ``` The [CLI calls the Kody Open API](../../packages/cli/src/cli.ts) with your token and passes the hosted URL as an app address, not as the request destination. With an `@org/app` address, an explicit `--org` must match that org; omit `--org` with a hosted URL. `KODY_ORG` and the stored active org do not set the app's owning org. Websocket upgrade requests (`Upgrade: websocket`, `Connection: Upgrade`, or equivalent) are rejected. ## Response | Field | Meaning | | --------------- | --------------------------------------------------------- | | `status` | HTTP status code | | `headers` | Response headers (safe allowlisted subset) | | `body` | Response body text, or base64 for binary content | | `body_encoding` | `text` or `base64` | | `truncated` | `true` when the body was cut to fit the response size cap | | `redirected` | `true` when `appFetch` followed at least one redirect | | `url` | Hosted URL of the request that returned this response | | `redirects` | Each redirect followed: `status`, `url`, and `location` | ## Redirects By default `appFetch` follows a 301, 302, 303, 307, or 308 whose `Location` is inside the same app, as a new request, up to 10 times. Like a browser, a 303 (and a 301 or 302 after a `POST`) becomes a `GET` with no body; 307 and 308 keep the method and body. Each hop is a real request to the app. A redirect to another site or another app is not followed: that 3xx comes back with its `Location` header. To check that a form save redirected, and where, read `redirects`: ```json { "status": 200, "redirected": true, "url": "https://board--acme.kody.run/done", "redirects": [ { "status": 303, "url": "https://board--acme.kody.run/save", "location": "https://board--acme.kody.run/done" } ] } ``` Pass `"redirect": "manual"` (CLI: `--redirect manual`) to get the 3xx itself, with `location` in `headers`. The CLI status line names each redirect it followed, such as `HTTP 200 after redirect 303 to https://board--acme.kody.run/done`. For MCP OAuth calls the body stops at 102,400 bytes, whatever `max_bytes` asks for. The Open API honors `max_bytes` up to 10,485,760; `kody app fetch --out ` asks for that and writes the body to the file, decoding binary responses from base64. Without `--out`, the CLI uses the default cap. A host-setup 500 (`Package app could not be prepared`) can include the underlying platform error in `cause` when you pass `synthetic: true` (CLI: `--synthetic`). Host-setup failures happen before the isolate starts, so they do not appear as `app_fetch` run errors. ## Identity The app sees the person: `metaGetCurrentUser()` and `X-Kody-Visitor` carry the same `user_id` as a browser visit. When a profile token makes the call they also carry `profile: { id, name }`; `profile` is `null` otherwise. The run record carries the profile chain, and the publisher's Visitors list names the profile under the person. A browser opened with an [open link](#open-link) is identified the same way. A visitor-need credential (`credential.visitor()`, `visitorNeed`) resolves with the caller's own narrowing. A profile reaches it only when its chain was granted Use on that credential, whatever the person allowed in a browser. ## Consent An outside visitor with no membership role is asked for "Run as me" once per app. The [serve path](../../packages/worker/src/package-runtime/package-app-serve.ts) rechecks their approval before serving. An eligible browser GET navigation redirects for confirmation; a trusted in-process dispatch gets a 403 instead. `appFetch` reports missing "Run as me" approval or missing consent for a required visitor credential as: ```json { "error": { "code": "app_consent_required", "message": "...", "details": { "consent": "run_as_me", "consent_url": "https://kody.codes/..." } } } ``` `consent` is `run_as_me` or `visitor_need` (with `need`). Give `consent_url` to the person to approve in a browser, then retry. The call itself does not record approval. If the person has consented but the acting profile lacks Use on the credential, the result has `status: 403` with `app_profile_credential_required` in `body` instead. The HTTP call itself still succeeds, so check the result's `status`. ## Open link `appOpenLink` returns a link that opens the app in the caller's own browser. Use it when an agent needs the rendered pages; use `appFetch` for programmatic calls. Both use the same [target resolution and visit authorization](../../packages/worker/src/mcp/capabilities/packages/app-visit-target.ts). See [Consent](#consent) if a person needs to approve the visit. - **Who may call it:** the same callers and the same `app:execute` check as `appFetch`, through interactive MCP, `execute`, the Open API (`POST /v1/apps/open-link`), and the CLI (`kody app open-link`). - **Input:** `app` (an `@org/app` address or hosted URL) or `package_id`, and an optional `path`. - **Result:** `url`, `app`, `expires_at`, `expires_in_seconds` (300), and `session_seconds` (3600). ### Link exchange and visit access - **The link** is a [signed token on the app's own host](../../packages/worker/src/app/package-app-open-link.node.test.ts), valid for 5 minutes and for one use. It is bound to that app and that host, so it is refused anywhere else. - **Opening it** sets the app's [host-only visit session cookie](../../packages/worker/src/app/package-app-open-link.node.test.ts) for 60 minutes and redirects to the same address without the token. The cookie carries the person plus the calling credential's scopes and profile chain. It is scoped to that one app and is never a Kody account session. - **Every request** under that cookie runs [the visit check `appFetch` runs](../../packages/worker/src/app/package-app-visit-authorization.ts) for that credential. A revoked or expired API token, an inactive profile, or a removed grant ends the authenticated visit. For MCP or CLI sign-ins there is no API-token record to recheck; the cookie still expires after 60 minutes. - **Consent** works as in [Consent](#consent): the call returns `app_consent_required` and mints no link. In the browser, a consent that becomes missing later is a 403 that names the `consent_url`, not a redirect. - **A refused link** (expired, used, wrong app or host, or access removed) returns a 403 page and sets no cookie, including on a public app. ## Synthetic smoke tests After a publish, `test_hints.app` gives an `appFetch({ ..., synthetic: true })` call to copy. The [trusted dispatch](../../packages/worker/src/package-runtime/package-app-synthetic.node.test.ts) adds `Kody-Synthetic: true` for that opt-in; a caller-supplied header is stripped. The handler still runs with real side effects unless app code deliberately guards them. Synthetic fetches do not count as visitor traffic. ## Semantics - Strips credential headers (`Cookie`, `Authorization`, and internal `X-Kody-*` headers) before the handler runs, the same rule as public package-app ingress. - Populates `packageContext` (`hostedUrl`, `appBasePath`) from the owning org and package name leaf. - Uses the browser serve path for package runtime, storage, and secret mounts. Handler runs are recorded as `app_fetch` in [Activity](./activity.md); real visits count in the publisher's Visitors list. - Bills like a browser visit: a member's visit bills the owning org, a shared cross-org visit bills the visitor's own org, and a public app bills its publisher. - Does **not** count toward package activation milestones. ## Platform assets under `/_assets/` `appFetch` goes through the same serve path as the browser, so on a host that serves platform client assets, `path: "/_assets/__version.json"` returns the current `clientModuleUrl` and `path: "/_assets/"` returns files from `kody.app.assets`, including previously served files eligible for [immutable asset lookup](../guides/package-apps.md#static-asset-caching-and-rollbacks). The fetch handler is never invoked for those paths. A 404 on `/_assets/__version.json` means the host does not serve client assets yet, not that the handler is broken; see [Troubleshooting](../guides/package-apps.md#troubleshooting). ## Related - [Using apps as an agent](../guides/agent-apps.md) - [Packages](./packages.md): package apps and `hosted_app_url` - [Profiles](./profiles.md): scoped identities and their grants - [Synthetic event dispatch](./synthetic-event-dispatch.md): post-publish smoke test for one subscription handler - [Package apps guide](../guides/package-apps.md): session handoff, asset URLs, lean forks - Decision: [Synthetic package requests](../contributing/decisions/0013-synthetic-package-requests.md)