# Importing contacts

An import adds many contacts to a mailing list in one request. The import runs in the background,
and you follow its progress with the v1 import endpoints, whether you started it with v1 or v2.

## Choose v1 or v2

The v2 [`import/mailinglist`](/api/import#import-file-to-mailing-list) takes a CSV or XLS file. It
can find the list by name, and creates the list and its folder if they do not exist yet.

The v1 [`importCSVToMailingList`](/api/list-imports#import-csv-to-mailing-list) and
[`importJSONToMailingList`](/api/list-imports#import-json-to-mailing-list) import CSV or JSON data
into a list that already exists. The CSV import can also leave out the LianaAutomation events.

The v2 import and `importCSVToMailingList` can both replace the current members of the list
(`truncate`), save the values as list-specific properties (`use_list_props`), and give the
contacts a consent (`consent_id` and `consent_lang`). The differences are:

{/* Equal v1 and v2 columns, whatever their content. */}

<div className="[&_table]:w-full [&_table]:table-fixed [&_th:first-child]:w-36">

|                             | v1                                                  | v2                                                          |
| --------------------------- | --------------------------------------------------- | ----------------------------------------------------------- |
| Target list                 | An existing list, by `list_id`                      | A list by `list_id`, or by `name`, created if not found     |
| Folder                      | Not set                                             | `folder`, such as `Customers/Finland`, created if not found |
| Data                        | CSV, raw or Base64-encoded, or a JSON array         | A CSV or XLS file, Base64-encoded                           |
| Extra options               | `disable_cem_events` and `use_duplicates`, CSV only | None                                                        |
| Response                    | `null` for CSV, `true` for JSON                     | The list ID                                                 |
| Missing email or SMS column | Found only when the import runs                     | Rejected in the response                                    |

</div>

`importJSONToMailingList` takes only `list_id` and the data, and always imports with the default
options.

## Prepare the data

CSV data uses `;` as the field separator and `"` as the enclosure character, and is encoded in
UTF-8 with Unix line endings (`\n`).

The first row names the columns. One of them must be the email column (`email`, `e-mail`, `mail`,
`e_mail` or `@mail`) or the `sms` column. The other columns are property names, matched
case-insensitively. A column whose name matches no existing property creates a new one, so check
the spelling, and do not use [reserved property names](./restrictions#reserved-property-names).

:::warning{title="Blocked addresses"}

The import checks the email addresses against a blocklist of addresses and domains. If even one
address is blocked, nothing is imported. Check the data first with
[`isCSVToMailingListAllowed`](/api/list-imports#check-csv-before-import). It returns `true` if no
address is blocked, and otherwise the blocked addresses as CSV for you to remove.

:::

## Follow the import

<Stepper>

1. **Start the import**

   The request returns before the import is done. An account runs one import at a time, so a new
   import may have to wait for earlier ones to finish.

1. **Wait for it to finish**

   Call [`getImportStatus`](/api/list-imports#get-import-status) with the list ID, for example
   every few seconds. It returns `0` while the import is waiting, then the number of lines
   processed so far, and `-1` when the import has finished.

1. **Read the results**

   [`getImportLog`](/api/list-imports#get-import-log) returns the counts of new, updated,
   duplicate and rejected records, and the records that were not imported. An empty log means
   that the import failed before it started, for example because of a blocked address or a
   missing email or SMS column. The next import to the same list replaces the log.

</Stepper>

:::note{title="v2 imports start later"}

The v2 import runs as a queued background job. Until the job starts, `getImportStatus` returns
`-1`, the same value as when no import is running, and `getImportLog` returns the log of the
previous import. So `-1` right after a v2 import does not mean that it has finished. v1 imports
are not affected.

:::

## Large imports

An import request fails if the account already has too many imports waiting. Split a large import
into batches, for example of 10,000 contacts, and start each batch after the previous one has
finished.
