How to import CSV Files with Next.JS

React component for importOK

In this article we are going to explore how we can use importOK to embed an import wizard into a Next.js application. When we are done, we will be able to upload both CSV and Excel files, map the columns, review and fix any validation errors and finally submit the uploaded data to our API.

Setup your fields

For the sake of simplicity, we will be importing a list of Contacts. Our app will be expecting four fields: first name last name email phone and country.

First, we need to install the React component for importOK.

1npm install @importok/javascript
2npm install @importok/react

Then to embed the import wizard, we can add the following into our page.

1import dynamic from 'next/dynamic';
2 
3const ImportokWizard = dynamic(() => import('@importok/react'), {
4 ssr: false
5});
6 
7export default function Home() {
8 /**
9 * Import fields to be mapped
10 * Check https://importok.io/docs/fields for more details
11 */
12 const fields = {
13 first_name: {
14 label: 'First Name'
15 },
16 last_name: {
17 label: 'Last Name'
18 },
19 email: {
20 label: 'Email'
21 },
22 phone: {
23 label: 'Phone'
24 },
25 country: {
26 label: 'Country'
27 }
28 };
29 
30 return (
31 <div className="App">
32 <ImportokWizard
33 title="importOK Example for NextJS"
34 fields={fields}
35 />
36 </div>
37 );
38}

The above also defines a list of fields which more or less describe our resource schema. Providing only the labels is sufficient and more than enough to get you started. Give it a go! At this stage you should be able to upload a CSV File and map the columns. Sweet eh?

Demo of CSV import with column mapping

Normalize and Cleanup your data

More often than not, the data provided by your end-users is not in perfect shape. Extra trailing spaces, incorrectly formatted phone numbers, and other frequent irregularities that take time and effort to resolve.

To improve this, we can use transformers to apply common fixes. The transformation happens right after the file is uploaded and before the validation. importOK provides a handfull of built-in transformers but if necessary you can also add your own.

Let's extend our example to trim any spaces, and capitalize both first and last name.

1import dynamic from 'next/dynamic';
2 
3const ImportokWizard = dynamic(() => import('@importok/react'), {
4 ssr: false
5});
6 
7export default function Home() {
8 /**
9 * Import fields to be mapped
10 * Check https://importok.io/docs/fields for more details
11 */
12 const fields = {
13 first_name: {
14 label: 'First Name',
15 transformers: 'trim|capitalize'
16 },
17 last_name: {
18 label: 'Last Name',
19 transformers: 'trim|capitalize'
20 },
21 email: {
22 label: 'Email',
23 transformers: 'trim'
24 },
25 phone: {
26 label: 'Phone',
27 transformers: 'trim'
28 },
29 country: {
30 label: 'Country',
31 transformers: 'trim'
32 }
33 };
34 
35 return (
36 <div className="App">
37 <ImportokWizard
38 title="importOK Example for Next.js"
39 fields={fields}
40 />
41 </div>
42 );
43}

Validate your data

While transformers can help auto-heal your data, and bring consistency, they don't cover all the cases. What if the email provided is invalid or the last name is blank?

Validators have been designed to address this problem and provide instead feedback to your end-users, so that they can fix these errors. Similar to transformers, there are some build-in validators and you can add your own as well.

Let's improve further our example to make the last name required, and ensure that both email and phone are in the right format.

1import dynamic from 'next/dynamic';
2 
3const ImportokWizard = dynamic(() => import('@importok/react'), {
4 ssr: false
5});
6 
7export default function Home() {
8 /**
9 * Import fields to be mapped
10 * Check https://importok.io/docs/fields for more details
11 */
12 const fields = {
13 first_name: {
14 label: 'First Name',
15 transformers: 'trim|capitalize'
16 },
17 last_name: {
18 label: 'Last Name',
19 transformers: 'trim|capitalize',
20 validators: 'required'
21 },
22 email: {
23 label: 'Email',
24 transformers: 'trim',
25 validators: 'email'
26 },
27 phone: {
28 label: 'Phone',
29 transformers: 'trim',
30 validators: 'phone'
31 },
32 country: {
33 label: 'Country',
34 transformers: 'trim',
35 validators: 'in:countries'
36 }
37 };
38 
39 return (
40 <div className="App">
41 <ImportokWizard
42 title="importOK Example for Next.js"
43 fields={fields}
44 />
45 </div>
46 );
47}

Here is how it looks like at this point. Please note that you can filter by error type and that you can adjust your data without leaving the import wizard.

Demo of data validation

Push data to your API

There are a couple of options available to push the normalized and validated data to your API. You can either choose to send individuals records in parallel or send the full data set.

Let's use the latter option for our example.

1import dynamic from 'next/dynamic';
2 
3const ImportokWizard = dynamic(() => import('@importok/react'), {
4 ssr: false
5});
6 
7export default function Home() {
8 /**
9 * Import fields to be mapped
10 * Check https://importok.io/docs/fields for more details
11 */
12 const fields = {
13 first_name: {
14 label: 'First Name',
15 transformers: 'trim|capitalize'
16 },
17 last_name: {
18 label: 'Last Name',
19 transformers: 'trim|capitalize',
20 validators: 'required'
21 },
22 email: {
23 label: 'Email',
24 transformers: 'trim',
25 validators: 'email'
26 },
27 phone: {
28 label: 'Phone',
29 transformers: 'trim',
30 validators: 'phone'
31 },
32 country: {
33 label: 'Country',
34 transformers: 'trim',
35 validators: 'in:countries'
36 }
37 };
38 
39 /**
40 * Push the normalized and validated records to the API
41 * Check https://importok.io/docs/webhooks for more details
42 */
43 const importRecords = async function (records, meta) {
44 return await fetch('https://your-api.com', {
45 method: 'POST',
46 headers: {
47 'Content-Type': 'application/json'
48 },
49 body: JSON.stringify(records)
50 });
51 };
52 
53 return (
54 <div className="App">
55 <ImportokWizard
56 title="importOK Example for Next.js"
57 fields={fields}
58 onImportReady={importRecords}
59 />
60 </div>
61 );
62}

Conclusion

importOK can be used to import data into your application, by embedding a powerful import wizard and leverage your existing API.

As a guideline, first you need to define the fields, so that your end users can map their data. Then you will need to define some transformers to normalize and clear the data to be imported. Next, you will need to define some validation rules so that you users can get instant feedback when something is wrong. Last, but not least, you will need register your webhooks so that you can actually import the data into your backend system.

importOK is available as a trial so that you can give it try before purchasing the pro license. At the moment of writing, you can join early and benefit from a 67% discount, for early adopters.

The easiest way to import data

Ready for your app

Get importOK

Free trial available