(Part 2) Update Records in NationBuild API using OpenRefine

This article is part of a series of tutorials to use NationBuilder API. We wrote them in collaboration with Campaign Gears. In the previous article, we explained how to retrieve people information via NationBuilder API. This article explains how to use OpenRefine to bulk update information via NationBuilder API.

Don't hesitate to contact us if you have questions or need help to implement NationBuilder data integration project.


For this tutorial like on the previous one you need to know and replace values for:
  • NATION_SLUG: name defined in NationalBuilder when you create your profiles in https://nationbuilder.com/create. 
  • NB_TOKEN: value defined by NationalBuilder when the new account is created. The token provides the credentials access to use NationalBuilder API.
You need to install cURL for windows. You can refer to this tutorial.

In this example, we:

  1. Prepare the data by downloading information from NationBuilder API,
  2. Edit them in OpenRefine,
  3. Upload our changes via NationBuilder API. 

Prepare the data

  1. Follow the steps from the Using cURL method described in the previous tutorial.
  2. Reorganize, delete and rename columns to better structure and order for the dataset. 
    • To edit the name of one column: Click in the icon of the column ''Edit column'' -> ''Rename this column''.
    • To move column: Click in the icon of the column ''Edit column'' -> ''Move column...''
    • To delete column: Click in the icon of the column ''Edit column'' -> ''Remove this column''
  3. Do the following edit:

    1. Rename the column ''_ - id'' to ''id
    2. Rename the column  ''_ - phone'' to ''phone''. 
    3. Move the ''first_name'' and ''last_name'' to the beginning for identifying each person.
  4. In the phone column, we need to distinguish between empty and null value. Changing the data type from number to text let us format the phone number correctly.  
    1. Click on column ''phone'' option ''Edit cells'' -> ''Common transforms'' -> ''To text''.
  5. Using the edit option next to each cell, make changes to last_name and phone

Update the records using NationBuilder API.

The following steps explain how to prepare the cURL command to update the changes we made to the last_name and phone.  
  1. From the column ''id'' select ''Edit column'' -> ''Add column based on this column...''
  2. In the new window paste the following GREL expression. Set the name for new column to ''cURL_command'' and press ''OK''. Don't forget to update the NATION_SLUG and NB_TOKEN.
    "curl -X PUT \"https://NATION_SLUG.nationbuilder.com/api/v1/people/"+value+"?access_token=NB_TOKEN\" -H \"Content-Type: application/json\" -d \"{\\\"person\\\":{\\\"phone\\\": \\\""+jsonize(cells["phone"].value) +"\\\", \\\"last_name\\\": \\\"new_last_name\\\"}}\""
    Above expression update ''phone'' and ''last_name'' based on the value in the column "phone" and "last_name"
    We use ''\' to escape special characters from the GREL expression and for cURL as follow:
    • ''\"'' is used for escaping character ''"'' from the GREL expression.
    • ''\\\"'' is used for escaping ''\"'' when you run the cURL command.
  3. The GREL expression handles the Null value as the full-text null. Replace the string name null to valid null values. Click on column ''cURL_command'' and ''Edit cells'' -> ''Transform...'' and paste the next expression:
  4. To export the new column to a file and execute it like a batch file press on the top right button ''Export'' -> ''Templating...''

  5. Prepare the template:
    1. Delete all content from Prefix, Row Template, Row Separator, Suffix. 
    2. Paste the following expression in the template in the area ''Row Template''. Do not forget to copy the extra line-break in the code.
    3. Press ''Export''.
  6. Change the extension from the downloaded file from ''.txt'' to ''.bat''. In our example, ''NationBuilder.txt'' become ''NationBuilder.bat''
  7. Open a terminal in the same location of the file and run the batch file using below command: NationBuilder.bat