# Dokicasa SDK: integration instructions for AI assistants

Give this file to an AI assistant to integrate the Dokicasa form into a partner's
website. Full guide: https://docs.dokicasa.it/en/form/sdk-javascript

If something is missing (form slug, API key, domains), ask the user. Don't make it up.

## Status: beta, activation on request

Available: filling in the form and sending the practice, for contracts and services.
Still being verified, test with Dokicasa before going live: attachments, address
autocomplete.

## How it works

- Web Component `<dokicasa-form>` in a single JS file, with Shadow DOM, no iframe.
- No new key: it uses the partner's API key, which is secret.
- The partner's server uses the API key to request a short-lived token (1 hour). Only
  that token goes in the page, and it works only on activated domains and only for
  the form.
- On completion the form calls `POST /api/v3/form/{slug}`, like the partner APIs: same
  validations, charge and webhooks. The practice is created only there: no drafts.

## Rules

1. The API key must NEVER reach the browser, a bundle or versioned code.
   `POST /api/v3/sdk/token` is called from the server only.
2. Domains must be given to Dokicasa first. The match is exact: `www.site.com` and
   `site.com` are two different domains.
3. There is no `api-key`, no `pk_live`, no Composer or npm package, no JWT to sign,
   and no `sdk.dokicasa.it` domain. Ignore them if you find them in old examples.

## Step 1: the token, from the server

```bash
curl -X POST https://app.dokicasa.it/api/v3/sdk/token \
  -H 'Authorization: Bearer API_KEY' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{"external_id": "partner-practice-id"}'
```

Response: `{ "user_token": "dksdk....", "expires_in": 3600 }`.
`external_id` is required (191 characters at most): same meaning as the partner APIs,
including resending with no double charge. Optional: `standalone`, `bundle_name`.
`401`: wrong API key. `403`: no domain activated. `422`: `external_id` missing.

## Step 2: the form, in the page

```html
<script src="https://app.dokicasa.it/contract-form-v2-sdk/v1.js"></script>
<dokicasa-form slug="FORM_SLUG" user-token="USER_TOKEN"></dokicasa-form>
```

### Which slug to use

Read `GET https://app.dokicasa.it/api/v3/catalog-forms` (public, as in the partner APIs).
Each entry has a `kind`:

- `contract` or `service`: a single form, use its `slug`. A service that only exists in a
  bundle returns `422 BUNDLE_NOT_STARTED`: to get it alone, use a token with
  `"standalone": true`.
- `bundle` (e.g. `canone-concordato-milano`): a multi-step process. The bundle's slug is NOT
  a form (`form_url: null`): use the slugs in `steps`, one at a time.

Bundle rules:

1. Start from step 1: the first form with a new `external_id` creates the practice. From an
   inner step you get `422 BUNDLE_NOT_STARTED`, with `first_step_slug`.
2. Same `external_id` for all steps, and one `<dokicasa-form>` per step.
3. Go in order: skipping a step returns `409`.
4. Several entries with the same step number are alternatives, use one (in the agreed-rent
   contract, at step 3: 3+2, transitional or students contract). Ask the user which.

For a complete agreed-rent practice use the city's bundle, not the single services. If it
isn't clear which process is needed, ask.

Attributes:

| Attribute | Required | Description |
|---|---|---|
| `slug` | yes | Form to show |
| `user-token` | yes | Token received from the server |
| `contract-type` | no | Variant, default `default` |
| `logo` | no | Logo URL, or `none`. Without it: Dokicasa logo |
| `api-base` | no | Default `https://app.dokicasa.it/api`, no need to change it |

## Events

DOM events on the element, data in `event.detail`:

| Event | When | `detail` |
|---|---|---|
| `load` | Form ready | `{ steps }` |
| `submitted` | Practice created | `{ id, externalId, bundleId }` |
| `error` | Request failed | `{ message, status }` |

The form never changes page by itself: it shows a confirmation, and the partner's page
decides what to do by listening to `submitted`. On `error` check `message` too: 401
means either an expired token (`Token SDK non valido o scaduto.`, get a new one and
reload) or insufficient balance.

## Colours and fonts

The site's CSS doesn't enter the Shadow DOM: you can't target inner elements. Customize
with CSS variables and `font-family` on the element:

```css
dokicasa-form {
  --primary: #062248;       /* headings, labels, main button */
  --primary-600: #0a325e;   /* main button, hover */
  --primary-50: #E6EAF1;    /* soft backgrounds */
  --secondary: #0dc9b7;     /* progress bar, accents */
  --secondary-600: #0aa697; /* accents, hover */
  --secondary-50: #E2F9F5;  /* soft accent backgrounds */
  --green: #00ba8e;         /* confirmations */
  --red: #e11d48;           /* errors, required fields */
  --ink-700: #1f2937;       /* text */
  --ink-50: #f8fafc;        /* light backgrounds */
  --muted: #6b7280;         /* secondary text */
  --border: #e5e7eb;        /* borders */
  --bg: #f5f7fb;            /* form background */
  font-family: 'Site font', sans-serif;
}
```

The values above are the defaults. Load the font from the partner's page.

## What's not in the SDK

Address book and land registry search by tax code: hidden on purpose. The token only
opens the form routes: the others return `403`. For anything else use the
server-to-server APIs with the API key: https://docs.dokicasa.it/en/form/server-to-server

## Errors in the form

- `401`: expired or invalid token, or insufficient balance on submission.
- `403`: domain not activated, operation not available from the SDK, or account not
  configured on submission.
- `422`: invalid fields, the form shows them. `409`: `external_id` already used by
  another practice, or bundle step not allowed.
- CORS error in the console: the page's domain isn't among the activated ones.

## Before calling it done

1. The API key appears in no file served to the browser.
2. The token is requested by the server each time the form opens.
3. The form appears on the activated domain, and the `load` event fires.
4. A submission fires `submitted`, and the practice shows up on the partner's account
   with its `external_id`.
5. The page handles `submitted` and `error`.
