Velo Tutorial: Importing and Exporting Collection Data with Code
In this article we will demonstrate how to import data into a collection and export data from a collection using code. We create a simple form and then write some code that imports and exports JSON data from your collections using the the wix-data
API.
Prerequisites
Since the wix-data
API requires that your data be in JSON format, you'll need to be familiar with converting your data to and from JSON.
- Use a spreadsheet or database application to export your data in the CSV format.
- Use an online tool, such as convertcsv.com, to convert your CSV data to the JSON format.
- Use the form as described below to import the JSON data into your collection.
- Use the form as described below to export the JSON data from your collection.
- Use an online tool, such as convertcsv.com, to convert your JSON data to the CSV format.
- Use a spreadsheet or database application to view or import your data from the CSV format.
Data Setup
Since we are going to use the wix-data
API to import data, your data will need to use the Field Key, and not the Field Name, to identify which fields you are using in your data source. Meaning, the first row of your CSV file should contain your collection's field keys.
If you want to provide your own IDs for the items you are adding into your collection, add a field with the key _id
that contains the ID values. If you don't provide your own ID values, each item will get a unique, auto-generated ID value.
Form

Type | ID | Usage |
---|---|---|
Text Input | collectionInput | For entering the collection to import to or export from |
Text Box | textBox | For entering data to be imported or for displaying data that has been exported |
Button | importButton | For triggering an import |
Button | exportButton | For triggering an export |
Code
In the Page Code panel, start by importing the wix-data
API as the very first line of code.
1import wixData from 'wix-data';
Import Code
In the Page Code panel, add the code below to the event handler:
1export function importButton_onClick(event) {
2 const items = JSON.parse($w("#textBox").value);
3 const collection = $w("#collectionInput").value;
4
5 items.forEach( (item) => {
6 wixData.insert(collection, item)
7 .then( (results) => {
8 console.log(`Added item: ${JSON.stringify(results)}`);
9 } )
10 .catch( (err) => {
11 console.log(err);
12 } );
13 } );
14
15 $w("#textBox").value = "";
16}
This code pulls the name of the collection and the JSON items from the form elements. It then loops through each JSON item and adds it to the collection using the insert()
function.
Export Code
In the Page Code panel, add the code below to the event handler:
1export function exportButton_onClick(event) {
2 let collection = $w("#collectionInput").value;
3
4 wixData.query(collection)
5 .find()
6 .then( (results) => {
7 $w("#textBox").value = JSON.stringify(results.items);
8 } )
9 .catch( (err) => {
10 console.log(err);
11 } );
12}
This code pulls the name of the collection from the form element and uses it to query the collection using the query()
and find()
functions.
1export function exportButton_onClick(event) {
2 let collection = $w("#collectionInput").value;
3
4 wixData.query(collection)
5 .limit(1000)
6 .find()
7 .then( (results) => {
8 $w("#textBox").value = JSON.stringify(results.items);
9 } )
10 .catch( (err) => {
11 console.log(err);
12 } );
13}
Using the Form
- Enter the name of the collection you want to import the data into.
- Paste the data in JSON format into the text box.
- Click the Import button.
- As each item is imported into the collection, a success or error message is logged to the console.
- Enter the name of the collection you want to export the data from.
- Click the Export button.
- The exported data is displayed in the text box or an error is logged to the console.
Additional Considerations
In that case you should consider if you want to publish your site to work with the Live collection or work with the Live collection through the Sandbox collection to avoid the potential security risk described above.
Using the form with the Live Collection without publishing your site
- Create the form and add the code.
- Copy the items from your Live collection to your Sandbox collection.
- Preview your site and use the form to export your data.
- Delete the page that contains the form before your next site publish.
To import data to your Live collection through the Sandbox:
- Create the form and add the code.
- Preview your site and use the form to import your data.
- Copy the imported items from your Sandbox to your Live collection.
- Delete the page that contains the form before your next site publish.
Using the form on your published site
- Create the form and add the code.
- Hide the form's page so it doesn't appear in your site's menu.
- Password protect the form's page so visitors can't access the page even if they have its URL.
- Add a Member Login button if necessary (see note below).
- Publish your site.
- Use the form when previewing your site to import and export using your Sandbox collection.
- Use the form on your published site to import and export using your Live collection.
API List
- $w.TextBox.value - Sets or gets an element's value.
- $w.TextInput.value - Sets or gets an element's value.
- wix-data.insert( ) - Adds an item to a collection.
- wix-data.query( ) - Creates a query.
- wix-data.WixDataQuery.find( ) - Returns the items that match a query.
- wix-data.WixDataQuery.limit( ) - Limits the number of items a query returns.