# Building automations

## Overview

An automation is a small piece of software that connects Aiwyn to another system your firm uses. When something happens in one, the automation notices and takes care of the follow-up in the other, so nobody has to do it by hand.

A couple of examples of what firms build:

- When a client completes their engagement in Aiwyn, record the outcome of a consent form against that client in your workflow system. [Tracking client consent](/guides/track-client-consent) walks through this one.
- When a vendor charges you for work on a client's return, add that charge to the client in Aiwyn as an expense, so it is billed through.


Aiwyn gives you the building blocks: an **API** to read and write Aiwyn data, and **webhooks** that tell you the moment something happens in Aiwyn. Your firm, or a developer you hire, writes the code that joins them to the other system and runs it.

Because the automation is yours, your firm owns the code, where it runs, and keeping it working. Aiwyn looks after the API and webhooks and is happy to answer questions about how they behave. For the other system, its vendor's own documentation is the best guide, since it stays current with their product.

If you're deciding whether to build an automation, this page and the checklist at the end are a good place to start. If you're the developer building it, read this page first, then the IDs, API, and webhooks guides linked at the bottom.

## How an automation works

Every automation follows the same three steps, whichever way the data moves: something triggers it, it gathers what it needs, then it acts.

The trigger comes from Aiwyn, your other system, or a schedule; the lookup and the write run in code your firm owns. 

1. **Trigger.** An event in one system starts the automation. Events usually carry IDs, not the whole record.
2. **Look up and match.** The automation uses those IDs to fetch the details it needs, and works out which record in the other system they belong to.
3. **Act.** The automation creates or updates the record in the other system, then records what it did so a failure can be found and retried.


The matching step is where most of the firm-specific work sits. Aiwyn and your other systems number clients and projects differently, so your automation needs a reliable way to find the same record in both. Your firm decides how to keep that lookup, and [Tracing IDs](/guides/ids-and-tracing) walks you through which Aiwyn identifiers to store.

## What you can start from, and what you can do in Aiwyn

Every automation starts with a trigger, and there are three kinds to choose from:

| Trigger | How it works | Good for |
|  --- | --- | --- |
| An Aiwyn webhook | Aiwyn calls a URL you host the moment an event happens (an engagement is signed, a payment is made). Each event is sent once, so pair it with a schedule that catches up. | Reacting quickly to something in Aiwyn. |
| The other system's webhook or trigger | The other system calls your automation when something happens there. Check that vendor's documentation for what it offers. | Reacting to something outside Aiwyn. |
| A schedule | Your automation runs on a timer and asks one or both systems what changed. | Systems with no webhooks, or work that can wait. |


Once triggered, these are the main things an automation can do in Aiwyn through the API. The full list, with every field, is in the [API reference](/bundle).

| Area | Read | Create or change |
|  --- | --- | --- |
| Clients and contacts | Clients, client groups, contacts, and contact details | Onboard a client; update a client's fields; create or update a contact |
| Engagements | Engagements and their documents, including each document's signature requirement and consent outcome | Create an engagement (one or in a batch); update an engagement |
| Projects and tasks | Projects, project templates, tasks, and task types | Create a project from a template; add or remove staff |
| Time and expenses | Time and expense entries, WIP, and work and expense codes | Create or update time and expense entries; create or update codes |
| Billing and payments | Invoices, unpaid invoices by client, draft invoice history, and payments | Create a payment link |
| Staff and firm setup | Staff, staff types, offices, departments, and service lines | Onboard or update staff; create staff types |
| Files | Files and their download links | None |
| Custom records | Your firm's custom record types and their schemas | Create, update, or delete records; create, update, or deactivate record types |


Don't see the action you need? Let your Aiwyn contact know. Requests for new endpoints are tracked and added to the API over time.

## Before you build

It's worth working through this list before anyone writes code. The first two items deserve the most thought.

- [ ] **Write the rule in one sentence.** "When X happens in system A, do Y in system B." If it takes a paragraph, it is probably two automations.
- [ ] **Decide how records match.** Know which field in each system identifies the same client, project, or engagement, and how you will look one up from the other. See [Tracing IDs](/guides/ids-and-tracing).
- [ ] **Decide what happens when it fails.** Who is told, and how the missed record gets fixed. A scheduled catch-up job covers anything a webhook missed.
- [ ] **Get API access in Aiwyn.** An API key with only the roles the automation needs. See [Using the API](/guides/using-the-api).
- [ ] **Get API access in the other system.** Through that vendor, under your firm's own agreement with them.
- [ ] **Build and test in the Aiwyn sandbox first**, against test data, before you point anything at production.
- [ ] **Decide where it runs.** A server, a cloud function, or an automation platform your firm already uses. It must be reachable from the internet if it receives webhooks.
- [ ] **Name an owner** at the firm who maintains it when either system changes.


## Related guides

- [Tracing IDs](/guides/ids-and-tracing): which identifiers to use and how records connect.
- [Using the API](/guides/using-the-api): getting a key, authenticating, and reading and writing data.
- [Using the webhooks](/guides/webhooks): subscribing to events, what Aiwyn sends, and handling failed deliveries and duplicates.
- [API reference](/bundle): every endpoint, field, and response.