Automatic CSV Import

Getting Started With Automatic CSV Import

User Management via automatic CSV import is one of the easiest and most scalable ways to manage your users on the Staffbase platform. The functions and capabilities that the automatic CSV import offer are the same as that of the manual CSV import performed in the Experience Studio. The only difference is that the script runs automatically on a schedule eliminating or reducing manual efforts. Similar to manual CSV import, for automatic CSV import, you need to actively push or upload the CSV file from your local environment to the Staffbase platform. You can do this with upload and update requests to the Staffbase endpoints. These calls are always performed in succession for the automatic CSV import.

Staffbase supports both complete and partial CSV automatic imports. The implementation for both processes is similar. If you use the partial CSV import, you need to define the partialImportTag parameter for it in the code.

Note: Contact support@staffbase.com to enable partial CSV import for your organization before implementing it.

Use this guide to get started with the automatic CSV import. The examples provided in the instruction use the cURL command-line.

Tip: If you are using PowerShell for automating your CSV import, contact support@staffbase.com to receive template scripts for using PowerShell v5 or higher.

Prerequisites

  • You have the user data in CSV file format.
  • The identifier for each user is defined in the CSV file.
    Note: If you already have existing users in the platform, ensure the identifiers are set and match the identifiers in the CSV file.
  • You have generated an API token with administrative access via the Experience Studio.
  • You have a good understanding of the manual CSV import via the Experience Studio.

Import Process Overview

The process of performing an automated CSV import in a nutshell.

  1. Upload: HTTP POST request to /user/import/csv/upload with the CSV file specified.
  2. Optional Preview: HTTP POST request to /user/import/csv/update with dry=true.
  3. Update: HTTP POST request to /user/import/csv/update with dry=false.

In this guide, the following CSV file is used:

External ID;First Name;Last Name;Email;Position;Department;Hire Date
A00123;John;Smith;john.smith@company.com;IT Manager;IT;01/05
A00124;Rosie;Jones;rosie.jones@company.com;Sales Representative;Sales;01/05

You can find more CSV file examples on our Support Portal.

1. Step: Upload the CSV File

Make an HTTP POST request to the endpoint: /user/import/csv/upload with the following details:

ParameterDescription
HostThe base URI for the hosting infrastructure on which your app is hosted.
AuthorizationThe API token to authorize the upload and update requests.
CSVThe path of the CSV file in your local environment.
EncodingThe character encoding used in the CSV file.
    Allowed values:
  • windows¬≠1252
  • utf-8
Field separatorThe field separator used in the file to separate the values.
    Allowed values:
  • comma (,)
  • semicolon (;)
Partial import tagThe parameter for the partial CSV import. This is required only if you want to implement a partial CSV import.

Example Request

The following cURL-command uploads a new CSV file named users_example.csv from the current directory, prettyprints the response (using json_pp) and stores it in upload.json.

1curl 'https://backend.staffbase.com/api/users/import/csv/upload' \
2 -X POST \
3 -H 'Authorization: Basic TOKEN' \
4 -H 'Content-Type: multipart/form-data' \
5 -F 'csv=@./users_example.csv' \
6 -F 'encoding=utf-8' \
7 -F 'fieldSeparator=";"' \
8 # optional parameter for partial import:
9 -F 'partialImportTag="csv_import:importTag"' \
10 --compressed | json_pp > upload.json

Tip: For debugging purposes, inspect the upload.json. For example, if you are using Linux environment, inspect the dry_run.json with cat dry_run.json.

Example Response

1{
2 "rowCount" : 2,
3 "total" : 7,
4 "columns" : [
5 "External ID",
6 "First Name",
7 "Last Name",
8 "Email",
9 "Position",
10 "Department",
11 "Hire Date"
12 ],
13 "data" : [
14 {
15 "title" : "External ID",
16 "rows" : [
17 "A00123",
18 "A00124"
19 ]
20 },
21 {
22 "title" : "First Name",
23 "rows" : [
24 "John",
25 "Rosie"
26 ]
27 },
28 {
29 "title" : "Last Name",
30 "rows" : [
31 "Smith",
32 "Jones"
33 ]
34 },
35 {
36 "title" : "Email",
37 "rows" : [
38 "john.smith@company.com",
39 "rosie.jones@company.com"
40 ]
41 },
42 {
43 "title" : "Position",
44 "rows" : [
45 "IT Manager",
46 "Sales Representative"
47 ]
48 },
49 {
50 "title" : "Department",
51 "rows" : [
52 "IT",
53 "Sales"
54 ]
55 },
56 {
57 "title" : "Hire Date",
58 "rows" : [
59 "01/05",
60 "01/05"
61 ]
62 }
63 ]
64}

2. Step: Map the Attributes and Preview the Import

Map the CSV attributes to the Experience Studio profile fields and upload the file to Staffbase using the HTTP POST method to the endpoint /user/import/csv/update.

On the CSV Import Mappings page you can find an overview of available attributes in the platform and how to map them with your CSV file.

Tip: You can map one column from your CSV file to multiple profile fields. For example [externalID,userName] maps the identifier and username based on the same column.

Note: Use the following parameters to:

  1. Display preview: Set the parameter dry to true in the code to display a preview. It is important to preview and test your mapping before the actual import to avoid incorrect syncs.
  2. Send emails or generate recovery codes: The following parameters are optional. By default, all three parameters are set to false.
    • sendMailsNew: Sends invitation emails to new users
    • sendMailsPending: Resends invitation emails to existing pending users
    • generateRecoveryCodes: Generates recovery codes for all users in the CSV file

Example Request

1curl 'https://backend.staffbase.com/api/users/import/csv/update' \
2 -X POST \
3 -H 'Authorization: Basic TOKEN' \
4 -H 'Content-Type: application/x-www-form-urlencoded' \
5 -d 'mappings=externalID,profile-field:firstName,profile-field:lastName,eMail,profile-field:position,profile-field:department,profile-field:hiredate' \
6 -d 'sendMailsNew=false' \
7 -d 'sendMailsPending=false' \
8 -d 'generateRecoveryCodes=false' \
9 -d 'dry=true' \
10 compressed | json_pp > dry_run.json

Tip: Preview the CSV to check all the changes you're about to make are correct. The preview contains all the changes, such as created, updated, or deactivated users. In addition it may contain, warnings or errors, if any. For example, if you are using Linux environment, inspect the dry_run.json with cat dry_run.json.

Example Response

1{
2 "allChanges" : {
3 "A00123" : {
4 "eMail" : {
5 "new" : "john.smith@company.com",
6 "old" : null
7 },
8 "importTags" : {
9 "new" : [
10 "csv_import:importTag"
11 ],
12 "old" : []
13 },
14 "locale" : {
15 "new" : "en_US",
16 "old" : null
17 },
18 "profile-department" : {
19 "new" : "IT",
20 "old" : null
21 },
22 "profile-firstName" : {
23 "new" : "John",
24 "old" : null
25 },
26 "profile-hiredate" : {
27 "new" : "01/05",
28 "old" : null
29 },
30 "profile-lastName" : {
31 "new" : "Smith",
32 "old" : null
33 },
34 "profile-position" : {
35 "new" : "IT Manager",
36 "old" : null
37 }
38 },
39 "A00124" : {
40 "eMail" : {
41 "new" : "rosie.jones@company.com",
42 "old" : null
43 },
44 "importTags" : {
45 "new" : [
46 "csv_import:importTag"
47 ],
48 "old" : []
49 },
50 "locale" : {
51 "new" : "en_US",
52 "old" : null
53 },
54 "profile-department" : {
55 "new" : "Sales",
56 "old" : null
57 },
58 "profile-firstName" : {
59 "new" : "Rosie",
60 "old" : null
61 },
62 "profile-hiredate" : {
63 "new" : "01/05",
64 "old" : null
65 },
66 "profile-lastName" : {
67 "new" : "Jones",
68 "old" : null
69 },
70 "profile-position" : {
71 "new" : "Sales Representative",
72 "old" : null
73 }
74 }
75 },
76 "created" : {
77 "data" : [
78 {
79 "config" : {
80 "locale" : "en_US"
81 },
82 "created" : "2021-06-21T10:00:00.000Z",
83 "emails" : [
84 {
85 "value" : "john.smith@company.com"
86 }
87 ],
88 "enabled" : true,
89 "externalID" : "A00123",
90 "pending" : true,
91 "profile" : {
92 "avatar" : null,
93 "department" : "IT",
94 "firstName" : "John",
95 "hiredate" : "01/05",
96 "lastName" : "Smith",
97 "location" : null,
98 "phoneNumber" : null,
99 "position" : "IT Manager",
100 "publicEmailAddress" : null
101 },
102 "role" : {
103 "type" : "reader"
104 },
105 "status" : "pending",
106 "tags" : [
107 "csv_import:importTag"
108 ],
109 "updated" : "2021-06-21T10:00:00.000Z"
110 },
111 {
112 "config" : {
113 "locale" : "en_US"
114 },
115 "created" : "2021-06-21T10:00:00.000Z",
116 "emails" : [
117 {
118 "value" : "rosie.jones@company.com"
119 }
120 ],
121 "enabled" : true,
122 "externalID" : "A00124",
123 "pending" : true,
124 "profile" : {
125 "avatar" : null,
126 "department" : "Sales",
127 "firstName" : "Rosie",
128 "hiredate" : "01/05",
129 "lastName" : "Jones",
130 "location" : null,
131 "phoneNumber" : null,
132 "position" : "Sales Representative",
133 "publicEmailAddress" : null
134 },
135 "role" : {
136 "type" : "reader"
137 },
138 "status" : "pending",
139 "tags" : [
140 "csv_import:importTag"
141 ],
142 "updated" : "2021-06-21T10:00:00.000Z"
143 }
144 ],
145 "total" : 2
146 },
147 "deactivated" : {
148 "total" : 0
149 },
150 "deleted" : {
151 "total" : 0
152 },
153 "updated" : {
154 "total" : 0
155 },
156 "userExternalIDToEmail" : {
157 "A00123" : "john.smith@company.com",
158 "A00124" : "rosie.jones@company.com"
159 },
160 "userExternalIDToNotificationEmailAddress" : {
161 "A00123" : "john.smith@company.com",
162 "A00124" : "rosie.jones@company.com"
163 },
164 "errors" : [],
165 "infos" : [],
166 "warnings" : [],
167 "newEmailInvitesCount" : 2,
168 "pendingEmailInvitesCount" : 0
169}

See also the CSV Import References page to help you analyze your CSV import and get more information on potential warning and error codes.

3. Step: Import the File

After testing the mapping, you can set the parameter dry to false and import the file.

Tip: Use the example request code snippet from step 2 and modify the dry parameter.

Example Request

1curl 'https://backend.staffbase.com/api/users/import/csv/update' \
2 -X POST \
3 -H 'Authorization: Basic TOKEN' \
4 -H 'Content-Type: application/x-www-form-urlencoded' \
5 -d 'mappings=externalID,profile-field:firstName,profile-field:lastName,eMail,profile-field:position,profile-field:department,profile-field:hiredate' \
6 -d 'sendMailsNew=false' \
7 -d 'sendMailsPending=false' \
8 -d 'generateRecoveryCodes=false' \
9 -d 'dry=false' \
10 compressed | json_pp > dry_run.json

Schedule the Imports

Schedule the imports and updates based on your business requirements. You can use any scheduling tool of your choice. For example, if you're using the cURL command-line for Unix or Linux, you can use the cron job command for scheduling.