# Zapier guide

> Create tests from your CRM and write the results back, with two Zaps and no code.

Source: https://lernexa.com/docs/developers/language-test/zapier

Two Zaps connect a CRM, such as Salesforce or HubSpot, to the Language Test:

1. **Create the test** when a record reaches the right stage, and store the candidate's link.
2. **Store the result** when the test is completed.

You need an [API key](https://lernexa.com/docs/developers/language-test/authentication) and a paid Zapier plan
(webhook steps are a premium feature).

## Zap 1: create the test

**Trigger:** your CRM's "updated record" trigger, for the moment a candidate needs a test, such as
an opportunity moving to a confirmed stage.

**Filter** (optional): only the languages you test, and only records with no test link yet.

**Action:** _Webhooks by Zapier_, event **POST**.

| Setting      | Value                                                       |
| ------------ | ----------------------------------------------------------- |
| URL          | `https://api-language-test.lernexa.com/api/v1/tests`        |
| Payload Type | **Json**                                                    |
| Data         | see below                                                   |
| Headers      | `Authorization`: `Bearer ltk_…` (your key)                  |

| Data key     | Value                                                                    |
| ------------ | ------------------------------------------------------------------------ |
| `name`       | the candidate's name field                                               |
| `email`      | the candidate's email field                                              |
| `language`   | the language field, holding names such as `French`                       |
| `externalId` | the record's id, with a suffix if one record has several candidates     |
| `sendEmail`  | `false` to send the link yourself, or leave it out to let us email it    |

:::caution
Set **Payload Type** to **Json**. With the default, Form, the request is refused with a sentence
saying so.
:::

**Action:** your CRM's "update record", writing the step's `url` into a field, such as
"Test link". Send it to the candidate from your CRM, or leave `sendEmail` out and we email it.

Zapier may run a step twice after a timeout. With `externalId` set, the second run returns the same
test and emails nobody.

## Zap 2: store the result

**Trigger:** _Webhooks by Zapier_, event **Catch Hook**. Copy the hook's URL, paste it into the
staff app's **Integrations** page under **Webhook**, press **Save**, then **Send a test event**.
Back in Zapier, **Test trigger** shows the sample to map fields from.

**Filter:** only continue if `event` exactly matches `test.completed`. Add a second path for
`test.placement_changed` if you want staff changes to the level written back too (update the level,
send no emails).

**Action:** your CRM's "find record", by `test.externalId`.

**Action:** "update record":

| CRM field         | Value                     |
| ----------------- | ------------------------- |
| Level             | `test.result.levelText`   |
| Result link       | `test.result.recordUrl`   |
| Test completed    | `true`                    |

**Actions** (optional): email the teacher or the agent the result link. The record shows levels
only, never the candidate's answers.

## Checking it works

1. Move a test record to the trigger stage. A test appears in the staff app's **Tests** list, and
   its link in your CRM.
2. Open the link and take the test to the end.
3. Within seconds, Zap 2 runs and the level is in your CRM. The test's page in the staff app shows
   the webhook as **Delivered**.
