JavaScript SDK <dokicasa-form> beta
The SDK shows the Dokicasa form directly on your website, with no iframe. Your users fill it in on your site and the form can take on your colours.
When completed it makes the same call as the partner APIs, POST /api/v3/form/{slug}: same validations, same charge, same webhooks. The practice is created on your account only at that moment: there are no drafts.
Beta: activation on request
Filling in the form and sending the practice are available. Attachments and address autocomplete are still being verified: test them with Dokicasa before going live.
How it works
You don't need a new key: you use your API key, the same one as the other APIs. It is secret though, and must never reach the browser. That's why there are three steps:
- Your server asks Dokicasa for a short-lived token, using the API key.
- Your server passes the token to the page.
- The page shows the form with that token, valid for one hour and only on your domains.
Practice list
To show your account's practices on your site there is the <dokicasa-practices> component, with a scope: practices token.
1. Activation
Tell Dokicasa the domains where you'll use the form, for example www.yoursite.com. The match is exact: www.yoursite.com and yoursite.com are two different domains, so list both if you use both.
Until at least one domain is set, the token request returns 403. Activation changes nothing for the API calls you already make.
2. The token
From your server, every time a user opens the form:
curl -X POST https://app.dokicasa.it/api/v3/sdk/token \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"external_id": "practice-123"}'{ "user_token": "dksdk.eyJpdiI6...", "expires_in": 3600 }The body is the partner APIs' metadata, and it's stored in the token: it can't be changed from the browser.
| Field | Required | Description |
|---|---|---|
external_id | yes | Your identifier for the practice (191 characters at most). A new submission with the same id and form returns the practice already created, with no second charge |
standalone | no | true for a practice on its own, outside the bundle |
bundle_name | no | Bundle name |
For bundle steps the rules in Server-to-server apply: same external_id and steps in order.
Don't call this endpoint from the browser
Your API key would end up in the page, and anyone could use it on your account.
3. The form in the page
<script src="https://app.dokicasa.it/contract-form-v2-sdk/v1.js"></script>
<dokicasa-form
slug="cedolare-secca-contratto-locazione"
user-token="dksdk.eyJpdiI6...">
</dokicasa-form>$token = Http::withToken(env('DOKICASA_API_KEY'))
->acceptJson()
->post('https://app.dokicasa.it/api/v3/sdk/token', ['external_id' => $practice->id])
->json('user_token');const res = await fetch('https://app.dokicasa.it/api/v3/sdk/token', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.DOKICASA_API_KEY}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ external_id: practice.id }),
})
const { user_token } = await res.json()// The token comes from your server. Load the script only once.
export default function Contract({ token }) {
return <dokicasa-form slug="cedolare-secca-contratto-locazione" user-token={token} />
}<template>
<dokicasa-form slug="cedolare-secca-contratto-locazione" :user-token="token" />
</template>
<!-- Declare dokicasa-form as a custom element in your compiler configuration -->Which form to use
The full list comes from a public call, the same as the partner APIs:
curl https://app.dokicasa.it/api/v3/catalog-forms -H 'Accept: application/json'Each entry has a kind, which tells you how to use it:
kind | What it is | What you pass to slug |
|---|---|---|
contract | A single contract | Its slug |
service | A single service | Its slug |
bundle | A multi-step process, for example the agreed-rent contract | The slug of each step, one at a time. The bundle's slug isn't a form: it has form_url: null |
Bundles, for example the agreed-rent contract
If you need to follow the whole practice use the bundle for the city, not the single services:
{
"kind": "bundle",
"name": "Canone Concordato Milano",
"slug": "canone-concordato-milano",
"form_url": null,
"steps": [
{ "step": 1, "type": "Contract", "slug": "foglio-caratteristiche-immobile-milano" },
{ "step": 2, "type": "Service", "slug": "calcolo-canone-concordato-contratto-locazione-milano" },
{ "step": 3, "type": "Contract", "slug": "locazione-3-2-canone-concordato-milano" },
{ "step": 3, "type": "Contract", "slug": "locazione-transitoria-canone-concordato-milano" },
{ "step": 3, "type": "Contract", "slug": "locazione-studenti-universitari-milano" },
{ "step": 4, "type": "Service", "slug": "creazione-documenti-canone-concordato-milano" }
]
}(Only the first steps and the main fields are shown here.)
The rules are those of the partner APIs:
- Start from step 1. The first form with a new
external_idcreates the bundle's practice. If you start from an inner step you get422 BUNDLE_NOT_STARTED, with the slug to start from infirst_step_slug. - Use the same
external_idfor all steps: that's what keeps them together. Request a token with thatexternal_idand show one<dokicasa-form>per step. - Go in order. Skipping a step returns
409. - Several entries with the same step number are alternatives: use one. In the example, at step 3 you choose the contract type: 3+2, transitional or students.
A single form
For a single contract or service use an entry with kind contract or service.
A service that only exists inside a bundle returns 422 BUNDLE_NOT_STARTED: if you really need it alone, request the token with "standalone": true.
All the forms
The same as GET /api/v3/catalog-forms:
Attributes
| Attribute | Required | Description |
|---|---|---|
slug | yes | The form to show |
user-token | yes | The token received from your server |
contract-type | no | Contract variant. Default: default |
logo | no | URL of your logo in the header, or none to show no logo. Without this attribute the Dokicasa logo appears |
api-base | no | API address. Default: https://app.dokicasa.it/api. You don't need to change it |
Events
The form notifies your page through DOM events. The data is in event.detail.
| Event | When | detail |
|---|---|---|
load | The form is ready | { steps } |
submitted | The practice was created | { id, externalId, bundleId } |
error | A request failed | { message, status } |
const form = document.querySelector('dokicasa-form')
form.addEventListener('submitted', (e) => {
// The form shows a confirmation and stays on the page: you decide where the user goes
window.location.href = '/thanks?practice=' + e.detail.id
})
form.addEventListener('error', (e) => {
// The status alone isn't enough: 401 also means insufficient balance, see Errors
console.error(e.detail.status, e.detail.message)
})Colours and fonts
The form is isolated with Shadow DOM: your site's CSS can't break the form, but for the same reason your normal rules don't reach it. To customize it, set these variables on the element:
dokicasa-form {
--primary: #7a1fa2;
--primary-600: #5e1680;
--primary-50: #f3e5f7;
--secondary: #ff6f00;
--secondary-600: #e65f00;
--secondary-50: #fff3e0;
font-family: 'Your font', sans-serif;
}| Variable | What it colours | Default |
|---|---|---|
--primary | Headings, labels, main button | #062248 |
--primary-600 | Main button on hover | #0a325e |
--primary-50 | Soft backgrounds of the main colour | #E6EAF1 |
--secondary | Progress bar and accents | #0dc9b7 |
--secondary-600 | Accents on hover | #0aa697 |
--secondary-50 | Soft backgrounds of the accents | #E2F9F5 |
--green | Confirmations | #00ba8e |
--red | Errors and required fields | #e11d48 |
--ink-700 | Text | #1f2937 |
--ink-50 | Light backgrounds | #f8fafc |
--muted | Secondary text | #6b7280 |
--border | Borders | #e5e7eb |
--bg | Form background | #f5f7fb |
Load the font from your page. White and the colours of warnings can't be customized.
What's not in the SDK
Compared to the form on the platform, two features are missing on purpose:
- Address book: your saved customers and properties aren't visible to people filling in the form on your site.
- Land registry search by tax code: it would use up your credits.
The token only works for form operations: it gives no access to the platform or to the other APIs.
Errors
| Status | Where | Meaning |
|---|---|---|
401 | Token request | Missing or wrong API key |
403 | Token request | No domain activated for your account |
401 | Form, Token SDK non valido o scaduto. | Get a new one and reload the page |
403 | Form | Domain not activated, or operation not available from the SDK |
422 | Submission | Invalid fields: the form shows them to the user |
401 / 403 | Submission | Insufficient balance or account not configured, as in the partner APIs |
409 | Submission | external_id already used by another practice, or bundle step not allowed |
| CORS error in the console | Form | The page's domain isn't among the activated ones |
After sending
From your server you can read the status and tasks of the practice as described in Server-to-server.