# Segments import

Use this feature to add or remove [UserPoint](https://developer.mediarithmics.io/user-points) from [segments](https://developer.mediarithmics.io/user-points/segments).

## How-to

1. Use the [bulk import ](https://developer.mediarithmics.io/data-streams/data-ingestion/bulk-processing)endpoints to create a [document import](https://developer.mediarithmics.io/data-streams/data-ingestion/bulk-processing/..#document-import) with the`USER_SEGMENT`document type and `APPLICATION_X_NDJSON` or `TEXT_CSV` mime type.
2. &#x20;Create an[ execution ](https://developer.mediarithmics.io/data-streams/data-ingestion/bulk-processing/..#create-an-execution)with your user segment commands formatted in `ndjson` or `csv` depending on the mime type you chose.

### User segment command

Each line in the uploaded file can have the following properties:

| operation                 | Enum (Mandatory)               | Either `UPDATE` or `DELETE`                                                                                                                                     |
| ------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| compartment\_id           | String (Optional)              | The Compartment ID acting as a [user identifier](https://developer.mediarithmics.io/user-points#user-identifiers) in correlation with the user account ID       |
| user\_account\_id         | String (Optional)              | The User Account ID acting as a [user identifier](https://developer.mediarithmics.io/user-points#user-identifiers) in correlation with the user account ID.     |
| email\_hash               | String (Optional)              | The Email Hash acting as a [user identifier](https://developer.mediarithmics.io/user-points#user-identifiers).                                                  |
| user\_agent\_id           | String (Optional)              | The User Agent ID acting as a [user identifier](https://developer.mediarithmics.io/user-points#user-identifiers).                                               |
| segment\_id               | String (Optional)              | The Id of the segment in which the User is inserted/deleted.                                                                                                    |
| segment\_ids              | List of strings (Optional)     | <p>The list of ids of segments in which the User is inserted/deleted. For instance<br><code>\["segment\_id\_1","segment\_id\_2"]</code></p>                     |
| segment\_technical\_name  | String (Optional)              | The technical name of the segment in which the User is inserted/deleted.                                                                                        |
| segment\_technical\_names | List of strings (Optional)     | <p>The list of technical names of segments in which the User is inserted/deleted. For instance<br><code>\["technical\_name\_1","technical\_name\_2"]</code></p> |
| expiration\_duration      | Integer (Mandatory)            | The number of minutes before the user will be removed from the segment. 0 means that the User will never leave the segment                                      |
| expiration\_ts            | Number (Optional)              | The timestamp of the expiration date of the User in the segment. A value of 0 means that the user will never leave the segment                                  |
| data\_bag                 | Escaped JSON String (Optional) | The data bag associated with the user/segment relationship                                                                                                      |

### Example

```bash
# Create the document import
curl -X POST \
  https://api.mediarithmics.com/v1/datamarts/<DATAMART_ID>/document_imports \
  -H 'Authorization: <YOUR_API_TOKEN>' \
  -H 'Content-Type: application/json' \
  -d '{
	"document_type": "USER_SEGMENT",
	"mime_type": "APPLICATION_X_NDJSON",
	"encoding": "utf-8",
	"name": "<YOUR_DOCUMENT_IMPORT_NAME>"
}'
```

```bash
# Create the execution
curl -X POST \
  https://api.mediarithmics.com/v1/datamarts/<DATAMART_ID>/document_imports/<DOCUMENT_IMPORT_ID>/executions \
  -H 'Authorization: <API_TOKEN>' \
  -H 'Content-Type: application/x-ndjson' \
  -d '{ 
        "operation": "UPDATE",
        "expiration_duration": <INTEGER>,
        "compartment_id": "<COMPARTMENT_ID>", 
        "user_account_id": "<USER_ACCOUNT_ID>",
        "segment_id": "<SEGMENT_ID>"
      }'
```

{% hint style="success" %}
You can have UPDATE and DELETE operations in the same file upload.

Please note, if not using `csv`, that the uploaded data is in `ndjson` and not `json`. That means the different profiles are not separated by commas, but by a line separator `\n`
{% endhint %}

{% hint style="success" %}
Use segment\_ids or segment\_technical\_names if you need to handle multiple segments for a single user.&#x20;
{% endhint %}
