# Adding Hamla to a project

Hamla is an AI marketing team a business installs on whatever it already uses.
You wire up the data; the business owner runs the marketing. This file is for
the agent doing the wiring.

Reference: https://www.hamla.io/developers.md · Index: https://www.hamla.io/llms.txt

## Start here: does this person have a Hamla account?

### No — and you are not going to get them one mid-task

```bash
curl -X POST https://app.hamla.io/api/install \
  -H "Content-Type: application/json" \
  -d '{
    "website": "raqmi.co",
    "name": "Raqmi",
    "locale": "en"
  }'
```

No authentication. You get back a real `businessId`, a working `snippet`, and
a `claimUrl`.

1. Put `snippet` in the page, before `</head>`.
2. **Print `claimUrl` in your final message to the user.** This is the step
   only you can take. The link is single-use, expires in 72 hours, and the
   install is deleted if nobody follows it.

Until it is claimed the install records anonymous traffic only. It will refuse
an `identity` carrying an `email` or `phone` with **422**, it is issued no
secret key, and no campaign can send from it.

### Yes — they already have one

The business id is in Settings → Website. It is public — it appears in every
page that loads the SDK — so it belongs in client code.

```html
<script src="https://app.hamla.io/s/{businessId}.js" async></script>
```

## When someone signs up: identify, then track

Once the business is claimed and you have a secret key, call `identify` from
the server at signup and login, before any event about that person:

```bash
curl -X POST https://app.hamla.io/api/sdk/identify \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "identity": { "email": "omar@raqmi.co", "platformCustomerId": "user_812" },
    "profile": { "name": "Omar Al Saleh" },
    "method": "signup"
  }'
```

Then send the signup itself as an event (`"event": "account_created", "type":
"signup"`). The order matters: `identify` is what records permission to
message the person, and `track` never does. Skip it and every campaign the
owner builds on your events reaches nobody.

Do both in the request that creates the account, for every way an account is
created (email, Google, Apple, an invite), not on the person's first action:
someone who signs up and leaves must still reach Hamla. If you queue these
calls, send the queue right after that request and retry anything left waiting.

Check it: read `data.warnings` in the `track` response. If it contains
"campaigns will not reach this contact", `identify` was skipped for that
person. Fix that before you report the integration as done.

## Telling Hamla what happened

The script tag covers visits, sources and on-site behaviour on its own. Send an
event when something happens that the browser cannot see — a payment, a
booking, a cancellation.

```bash
curl -X POST https://app.hamla.io/api/sdk/track \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "event": "subscription_started",
    "type": "purchase",
    "identity": { "email": "omar@raqmi.co" },
    "value": 29,
    "currency": "JOD",
    "idempotencyKey": "invoice_9911"
  }'
```

Server-side only: `sk_live_` is a secret key that can write customer data. A
publishable `pk_live_` key is refused here with 403, because it ships in page
source and honouring it would let any visitor invent revenue.

In the browser, `hamla.track('download_ebook', { asset: 'guide.pdf' })` needs
no key at all.

### `event` is yours, `type` is Hamla's

Name the event whatever your domain calls it, in whatever language. `type` is
the vocabulary Hamla acts on:

| type | means | effect |
| --- | --- | --- |
| `purchase` | Money received | Adds to revenue, counts as an order |
| `refund` | Money returned | Subtracts from revenue |
| `booking` | Committed, not yet paid | No revenue |
| `lead` | Identity captured | No revenue |
| `checkout_started` | Started, not finished | No revenue |
| `fulfilled` | They received it | No revenue |
| `cancelled` | It ended | No revenue |
| `signup` | Account created | No revenue |
| `trial` | Free trial started | No revenue |
| `contact` | Reached out | No revenue |
| `application` | Applied | No revenue |

Omit `type` and Hamla infers it from the name and the money. Send it when you
know it.

## Make the events start campaigns

Events alone send nothing. You are not done until every moment that should
reach someone has a campaign listening for it. Connect the MCP server
(https://app.hamla.io/mcp, OAuth; the owner signs in), then for each such event:

1. `createCampaign` with trigger `app_event`, `connectionType`
   `hamla/events` and `eventName` exactly as your code sends it. Write the
   message for the person in that event's `identity`.
2. `requestCampaignLaunch`. It returns a link; the owner approves the launch
   in Hamla. You never launch on your own.
3. Fire the event once for a test person and confirm with `queryContacts`
   that it reached their timeline.

No MCP in your environment? Give the owner the list of events and what each
should send, so they can ask Hamla to build them.

Use the event in the message: each key of its `properties` is
`{{event.<key>}}` (properties.appName → `{{event.appName}}`). Send every
property a message will show from the first version (the other person's first
name, the item, a link): a campaign is checked against the latest real event,
so a property added later can't be used until an event carrying it arrives. A
link property must always have a value; send a general page when there is
nothing specific. Each event
sends its own message, right away, every time it happens. Campaigns that start
from a segment or a schedule are paced: a limited number of automatic
campaigns per person per day and week across the business
(https://docs.hamla.io/reference/timing), so start a promotion that should
wait its turn from a segment. Launching needs an active plan.

## Do not

- **Do not invent or guess a `businessId`.** If you do not have one, call
  `POST /api/install` above. A placeholder silently collects nothing.
- **Do not leave a `// TODO: get the Hamla business id` comment.** That is the
  failure `POST /api/install` exists to remove.
- **Do not put `sk_live_` in client code**, an env var prefixed `NEXT_PUBLIC_`,
  or anything a browser downloads.
- **Do not wire up identify or track until the business is claimed.** Before
  the claim no contact is stored: an email or phone is refused with 422 and your
  own `platformCustomerId` with 403. Add the snippet, print the claim link,
  and add the server calls once the owner has claimed it and created a key.
- **Do not describe a per-business webhook URL.** There is no such product. The
  server-side door is `POST /api/sdk/track`.
- **Do not report done from `success: true` alone.** Sign up a brand-new
  account through each sign-up path and do nothing else; each must appear in
  Hamla within a minute.
- **Do not record a signup with `track` alone.** Call `POST /api/sdk/identify`
  first. A contact created only by an event has no permission on record, and no
  campaign will message them.

## More

- https://www.hamla.io/developers.md — every field and option
- https://docs.hamla.io/reference/events — every error status
- https://app.hamla.io/mcp — MCP connector (OAuth), registry name `io.hamla/hamla`
- `npm install @gethamla/node` — the same HTTP API with queueing and retries

Handles: `usehamla` on GitHub, `@gethamla` on npm, `hamla.io` on the web.
