# Get a webhook on the last business day of the month: a five-minute AnyMoment quickstart

You will create a rule like "last business day of every month, 09:00 Madrid time, US holidays". AnyMoment will POST a signed `occurrence.due` webhook to your URL on that day and no other day. It works with anything that can receive an HTTPS POST: an n8n Webhook node, a Zapier Catch Hook, Make, a Cloudflare Worker, your own server.

> **Status of this guide (12 Nov 2026):** the endpoints, field names, headers and payload shape come from the live OpenAPI at `https://api.anymoment.sineways.tech/openapi.json` and the API source on `main`. **No step has been run end-to-end against production yet.** Steps marked ⚠️ are the least certain. Webhooks are API-only (there's no webhook screen in the app yet), and they're a Pro feature. New accounts get 30 days of Pro, then move to Free, where webhooks pause.

What you need: `curl`, a terminal, and a public **HTTPS** URL that accepts POST requests. For a first test, a https://webhook.site URL is fine. Plain `http://`, `localhost`, `*.local` and private IPs are rejected.

```bash
API=https://api.anymoment.sineways.tech
HOOK=https://webhook.site/your-unique-id     # or your n8n production webhook URL
```

## 1. Create an account and get a token (1 min)

Sign up at https://anymoment.sineways.tech/?ref=seo-webhook-quickstart and confirm your email if asked. Then get a session token with your email and password:

```bash
TOKEN=$(curl -s -X POST "$API/auth/token" \
  -H 'Content-Type: application/json' \
  -d '{"email":"you@example.com","password":"your-password"}' | tr -d '"')
```

Every call below sends it as the `x-auth-token` header.
⚠️ If you signed up with Google you have no password. Use an API key from the app instead. API keys are sent as `Authorization: Bearer am_live_…`. But an API key cannot create calendars, so create the calendar in the app, and give the key the scopes `events:write`, `events:read` and `webhooks:manage`.

## 2. Create a calendar (15 s)

```bash
CAL=$(curl -s -X POST "$API/calendars" -H "x-auth-token: $TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{"name":"Month-end automations","timezone":"Europe/Madrid"}' \
  | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
echo $CAL
```

## 3. Subscribe your URL to the calendar (15 s)

```bash
curl -s -X POST "$API/calendars/$CAL/subscriptions" -H "x-auth-token: $TOKEN" \
  -H 'Content-Type: application/json' \
  -d "{\"url\":\"$HOOK\",\"event_types\":[\"occurrence.due\",\"event.created\"]}"
```

The response contains a generated `secret`. **It's shown once, so save it.** You'll need it to verify signatures. Optional: `"occurrence_offset_seconds": -3600` fires one hour early (the limit is ±7 days).

## 4. Create the "last business day" rule and add it to the calendar (1 min)

```bash
EVT=$(curl -s -X POST "$API/events" -H "x-auth-token: $TOKEN" \
  -H 'Content-Type: application/json' -d '{
  "name": "Month-end close",
  "components": [{
    "operator": "OR",
    "event_config": {
      "recurrence": 2,
      "start_time": "09:00",
      "timezone": "Europe/Madrid",
      "config": { "interval": 1, "type": "last_business_day" }
    }
  }],
  "business_calendar": { "country": "US" }
}' | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')

curl -s -X POST "$API/calendars/$CAL/events/$EVT" -H "x-auth-token: $TOKEN" \
  -H 'Content-Type: application/json' -d '{}'
```

`recurrence: 2` means monthly. Other options:
- **First business day:** `"config": {"interval": 1, "type": "nth_business_day", "n": 1}`
- **3rd business day from the end:** `"type": "nth_business_day", "n": 3, "from_end": true`
- **Your own work week:** `"business_calendar": {"country": "IL", "weekend_days": [4, 5]}` (0 = Monday … 6 = Sunday, so `[4,5]` is Fri-Sat). Add `"subdiv"` for regional holidays (e.g. `"GB"` + `"ENG"`), `"closures": ["2026-12-24"]` for extra days off, and `"extra_workdays"` for working Saturdays.
- **A fixed day that moves off weekends and holidays:** use `"type": "day_of_month", "day": 25` and put `"roll": "backward"` at the top level next to `business_calendar`. Other values: `forward`, `nearest`, `skip`, `none`.

## 5. Check the dates before trusting it (30 s)

```bash
curl -s "$API/events/$EVT/instances?start=2026-11-01T00:00:00Z&end=2027-06-01T00:00:00Z&optimized=true" \
  -H "x-auth-token: $TOKEN"
```

For the US calendar above, you should get Mon 30 Nov 2026, Thu 31 Dec, Fri 29 Jan 2027, Fri 26 Feb, Wed 31 Mar, Fri 30 Apr, and **Fri 28 May 2027**. Mon 31 May is Memorial Day. (Expected dates come from python-holidays 0.108; ⚠️ not yet compared against production output.) Times come back in UTC, so 09:00 Madrid in winter is `08:00Z`.

## 6. What arrives at your URL

`occurrence.due` deliveries are queued up to about 48 hours ahead and sent at the occurrence time plus your offset. For a monthly rule, that means nothing shows up until two days before. Because you also subscribed to `event.created`, ⚠️ you may see an `event.created` delivery once the event is in the calendar (which change types fire when an event is *linked*, rather than created inside the calendar, isn't verified). To see a real `occurrence.due` within minutes, create a one-off test event: `"recurrence": 4`, `"config": {"scheduled_datetime": "<now + 5 min, local time>"}`. Then add it to the calendar the same way.

Headers:
```
Content-Type: application/json
User-Agent: AnyMoment-Webhooks/1
X-AnyMoment-Signature: <hex HMAC-SHA256>
X-AnyMoment-Timestamp: <unix seconds>
X-AnyMoment-Delivery: <delivery uuid>
```

Body (version 1, keys sorted, compact JSON):
```json
{"calendar_id":"…","change_type":"occurrence.due","event":{"description":null,"id":"…","is_active":true,"location":null,"name":"Month-end close","timezone":"Europe/Madrid"},"event_id":"…","occurrence_time":"2026-11-30T08:00:00+00:00","version":1}
```

Return any 2xx. Failures are retried up to 8 attempts. A 4xx (other than 408/429) is treated as permanent. See past deliveries and re-send one:

```bash
curl -s "$API/calendars/$CAL/subscriptions/<subscription_id>/deliveries" -H "x-auth-token: $TOKEN"
curl -s -X POST "$API/calendars/$CAL/subscriptions/<subscription_id>/deliveries/<delivery_id>/replay" -H "x-auth-token: $TOKEN"
```

## 7. Verify the signature

The signature is `HMAC_SHA256(secret, timestamp + "." + raw_body)` in hex. Check it against the **raw** request body, not a re-serialized one:

```python
import hmac, hashlib, time
def verify(secret: str, timestamp: str, raw_body: bytes, signature: str) -> bool:
    if abs(time.time() - int(timestamp)) > 300:   # reject replays older than 5 min
        return False
    expected = hmac.new(secret.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature)
```

In n8n: use a Webhook node (POST, production URL) with the **Raw Body** option on, then a Code node that does the same HMAC with `crypto`. ⚠️ The exact n8n raw-body field name depends on your n8n version.
