# Match an incoming email to a customer

Use this recipe when an email arrives, for example in a shared sales
mailbox or a help desk, and you want to show who sent it and what they
have open in Omono. You find the customer from the sender's address, then
read their open quotes, orders and recent invoices, and any document the
email quotes by number.

Every request in this recipe sends your API key in an
`Authorization: Bearer <key>` header. Paths are relative to
`https://<host>/api/v1`, and the examples show the full path. This recipe
only reads, but `GET /contacts`, `GET /invoices` and `GET /dispatches` are
in beta, so they can change in any way, at any time, until they're marked
stable. Learn more about
[beta operations](/docs/compatibility#stable-and-beta-operations).

You need the scopes `companies:read` and `sales:read`.

## Find the customer

Try each step in turn, and stop at the first one that finds a match.

1. **Look up the exact address.**

   ```http
   GET /api/v1/contacts?email=priya.shah@acme-engineering.example
   ```

   The match ignores capital letters. Each contact's `companyId` is the
   customer. Several contacts can share an address, for example a person
   who buys for two companies, so if they belong to different companies,
   let a person choose.

2. **Look up the domain.** If no contact has the address, send the part
   from the `@` in `search`:

   ```http
   GET /api/v1/contacts?search=@acme-engineering.example
   ```

   A text that contains `@` matches any part of the email address, so
   this finds everyone at that domain. Take the `companyId` they share.
   Skip this step for addresses at public email services, such as
   `gmail.com`, where people at many customers share a domain.

3. **Look up the company by name.** If you still have no match, search
   the companies with a name from the email, such as the sender's
   organisation in their signature:

   ```http
   GET /api/v1/companies?search=acme&isCustomer=true
   ```

   The search matches the company name, account number and postcode.
   Show the best matches to a person, rather than choosing one for them.

If nothing matches, the sender may be a new customer. Hand the email to a
person, or create the company as
[Sync customers and contacts from a CRM](/docs/recipes/sync-customers)
shows.

## How search works

Search works the way the search screens in Omono work:

- Each word of your text matches the start of a word, ignoring capital
  letters. `acm` finds Acme Engineering Ltd, but `cme` doesn't.
- The best matches come first. There's no cursor: you get up to `limit`
  records, and `hasMore` is `true` when more records match, so send a
  longer text to narrow the list.
- Search reads a copy of your records that Omono keeps for searching, so a
  record that was just created or changed can take a few seconds to
  appear.

Learn more about [search](/docs/reading#search).

## Show what the customer has open

With the customer's `id`, read their open quotes and orders, and their
latest invoices:

```http
GET /api/v1/quotes?companyId=<id>&status=draft,active
GET /api/v1/sales-orders?companyId=<id>&status=draft,on_order,on_hold,on_order_call_off
GET /api/v1/invoices?companyId=<id>&limit=10
```

Each list shows the newest records first.

## Find a document the email quotes

If the email quotes a number, find the document it names. Send the number
without the prefix that Omono shows:

| The email quotes                   | Send                                                              |
| ---------------------------------- | ----------------------------------------------------------------- |
| A quote, such as SQ-2210           | `GET /api/v1/quotes?quoteNo=2210`                                 |
| A sales order, such as SO-1042     | `GET /api/v1/sales-orders?orderNo=1042`                           |
| The customer's own order reference | `GET /api/v1/sales-orders?customerOrderNo=PO-7731&companyId=<id>` |
| An invoice, such as INV-20417      | `GET /api/v1/invoices?invoiceNo=20417`                            |
| A dispatch, such as SO-1042-2      | `GET /api/v1/dispatches?salesOrderId=<order id>&dispatchNo=2`     |
| A tracking number                  | `GET /api/v1/dispatches?trackingNo=JD014600003GB`                 |

A dispatch's `dispatchNo` counts within its sales order, so `dispatchNo`
needs `salesOrderId`: without it, the request fails with a `422` error.
Find the order with `orderNo` first, and send its ID in `salesOrderId`. A tracking number
matches only the whole text that staff saved: its capital letters, and
any spaces inside it, must be as stored.

Anyone can quote a number in an email. Check that the document's
`companyId` is the customer you found before you show it.

## Errors to handle

| Status | Code                                                              | What to do                                                                                                                                                                                                                        |
| ------ | ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 422    | [`validation-failed`](/docs/errors/validation-failed)             | A query parameter is wrong, and `errors[].field` names it. For example, a `search` text is shorter than 2 characters, or you sent `search` with `cursor`, `sort`, `updated_since`, `email` or another filter it can't take.       |
| 503    | [`temporarily-unavailable`](/docs/errors/temporarily-unavailable) | Retry after the number of seconds in `Retry-After`. On a request with `search`, it means Omono's search isn't available just now. The lookups without `search` still work, so you can hand the email to a person in the meantime. |
| 429    | [`rate-limited`](/docs/errors/rate-limited)                       | Retry after the number of seconds in `Retry-After`.                                                                                                                                                                               |
