All Collections
Import Scripts
Examples
Import data from Typeform
Import data from Typeform

Example Javascript code for importing response data from Typeform

Abbey Lauren Minondo avatar
Written by Abbey Lauren Minondo
Updated over a week ago

You can use Import Scripts to bring response data from your Typeform surveys directly into an Equals workbook.

1. Create a Typeform API Key and locate your Form Id

To find your API key, navigate to your personal settings and select the button that says "Generate a new token." To find your form Id, click into your form and copy the Id from the URL. For example, in the URL "https://mysite.typeform.com/to/u6nXL7" the form_id is u6nXL7.

2. Add the key to a secret group

Copy the the API key and form Id from the prior step. Then create a new secret group and paste the key into a new secret named typeform_api_key and typeform_form_id.

3. The code

Once you've completed the prior steps, create a new workbook, add a new import script to a sheet and copy + paste the script below. Once pasted select your secret group in the toolbar (make sure the key in the secret group is called typeform_api_key and typeform_form_id).

You'll want to modify the code below to reflect type of results you're importing.

const equals = require("equals");
const axios = require("axios");

const API_URL = "https://api.typeform.com/forms";
const API_KEY = equals.getSecret("typeform_api_key");
const FORM_ID = equals.getSecret("typeform_form_id");

const getFormData = async () => {
const resp = await axios({
method: "get",
url: `${API_URL}/${FORM_ID}/responses`,
headers: {
Authorization: `Bearer ${API_KEY}`,
},
params: {
page_size: 96,
},
});

return resp.data;
}

const formData = await getFormData();

// Create an array of column headers based on the keys of the first response
if (formData.items && formData.items.length > 0) {
const headers = Object.keys(formData.items[0].answers);
equals.addHeaders(headers);

// Add a row for each response, extracting the text value from each field object
for (const response of formData.items) {
const rowData = Object.values(response.answers).map((field) => {
if (field.type && (field.type === "text" || field.type === "email" || field.type === "choice" || field.type === "number" || field.type === "date")) {
if (field.type === "text" || field.type === "number" || field.type === "date") {
return field.text ? field.text : "";
} else if (field.type === "email") {
return field.email;
} else if (field.type === "choice" && field.choices && field.choices.length > 0) {
// The selected choice is stored in the first item of the choices array
return field.choices[0].text;
} else {
return "";
}
}
});
equals.addRow(rowData);
}
}


โ€‹

Did this answer your question?