Data Quick Start
Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.
The SDK data
module allows you to take advantage of Wix content management capabilities in a site or app you build on any platform. This means you can access and manage content in a Wix project from your site's code, as well as from the Content Management System (CMS) in the Wix dashboard.
This tutorial shows you how to create a React component that:
- Retrieves content from a data collection in a Wix project's database.
- Uses the content retrieved in a dynamic UI.
The tutorial is based on the Wix Headless example site. You can test out the live example site, or fork the site's code repo to use as a starting point for your own site or app. The component described in this tutorial is the list of examples at the bottom the page.
This implementation focuses on simplicity and understandability, rather than feature richness, performance or completeness. For details about additional functionality, see Wix Data in the API Reference. Looking for a more comprehensive example site integrating Wix Headless APIs for data management? Check out our starter templates.
Note: The code in this tutorial is written in JSX, but you can use the SDK in any JavaScript environment.
Implementing the Wix Data flow includes the following steps:
- Set up the Wix Headless environment.
- Add content in the CMS.
- Import the SDK modules and create an SDK client.
- Create a React component and a state variable.
- Define a function to fetch data.
- Add the
useEffect
hook. - Render the UI.
Step 1: Set up the Wix Headless environment
Before using the SDK, there are a few things you need to set up on your Wix account and in your external site or app's coding environment.
To set up the Wix Headless environment, follow these steps:
-
If you haven't already, create a project.
When prompted to add functionalities to your new project, you don't need to add any business solutions. Every Wix Headless project comes with CMS support installed. -
Set up authorization for your site by creating and configuring an OAuth app.
-
Install the SDK and relevant SDK module packages by running the following commands:
For NPM:
Copy1npm install @wix/sdk2npm install @wix/dataFor Yarn:
Copy1yarn add @wix/sdk2yarn add @wix/data -
Install the
react
package to handle UI rendering and thejs-cookie
package to handle session cookies.For NPM:
Copy1npm install react2npm install js-cookieFor Yarn:
Copy1yarn add react2yarn add js-cookie
Step 2: Add content in the CMS
Follow these steps to create a collection and add content to it:
-
Open the CMS in the project dashboard.
-
Click Create Collection.
-
Enter a name and ID for your collection, select Multiple items (Default), then click Create.
For this example, create a collection with the ID
examples
. -
You're now taken to the
examples
collection page. Click Manage Fields. -
Add fields to your collection.
For this example, ensure you include text fields with the IDs
title
,description
, andslug
, and a numerical field with the IDorderId
: -
In the collection page, enter the initial content for your collection.
Learn more about managing data using the CMS.
Step 3: Import the SDK modules and create an SDK client
The next step is to set up your code file to run the SDK functions. To set up the code file, follow these steps:
-
Add the following import statements to the top of your code file:
Copy1import Link from 'next/link';2import Cookies from 'js-cookie';3import { useEffect, useState } from 'react';45import { createClient, OAuthStrategy } from '@wix/sdk';6import { items } from '@wix/data'; -
Create an SDK client by adding the following code to your code file. Replace the value for
clientId
with your OAuth app's client ID. You can find the ID in your project's Headless Settings menu.
The value fortokens
is the'session'
cookie on the visitor's browser. It's used to make calls to the Wix API. This way, you can maintain previous visitor sessions. For information about managing cookies, see Session Token Management.Copy1const myWixClient = createClient({2modules: { items },3auth: OAuthStrategy({4clientId: `<YOUR-CLIENT-ID>`,5tokens: JSON.parse(Cookies.get('session') || null),6}),7});
Step 4: Create a React component and a state variable
The logic for our example content retrieval and rendering flow is contained in a React component called Examples
. To create the component, follow these steps:
-
Define the component function as a default export in your code file:
Copy1export default function Examples() {} -
Define a state variable by adding the following code in the component function:
Copy1const [examples, setExamples] = useState([]);In the steps that follow, the
examples
state variable stores the data retrieved by querying the Wix project'sexamples
collection.
Step 5: Define a function to fetch data
Define a function to fetch the data you need and save it to the state variable, by adding the following code to the component function:
1async function fetchExamples() {2 const examples = await myWixClient.items3 .queryDataItems({ dataCollectionId: 'examples' })4 .ascending('orderId')5 .find();6 setExamples(examples.items);7}
This function uses queryDataItems()
with chained DataItemsQueryBuilder
functions to retrieve all items in the examples
collection, sorted by orderId
in ascending order. The function then stores the resulting items in the examples
state variable.
Step 6: Add the useEffect
hook
Add the following code to the Examples
component to run the fetchExamples()
function after the component is rendered. This ensures that the data is retrieved when the component mounts.
1useEffect(() => {2 fetchExamples();3}, []);
Step 7: Render the UI
You can now render the data you retrieved in the component's dynamic UI. Add the following code to the Examples
component function's return
statement to render the UI. This code renders a card for each item stored in the examples
state variable. Each card displays the title
and description
for an item and links to its slug
.
1return (2 <footer>3 {examples.map((example) => (4 <Link href={example.data.slug} key={example._id}>5 <section>6 <h2>7 {example.data.title} <span>-></span>8 </h2>9 <p>{example.data.description}</p>10 </section>11 </Link>12 ))}13 </footer>14);
Complete code example
You can use the following full code example as a starting point for developing your own site:
1import Cookies from 'js-cookie';2import { useEffect, useState } from 'react';3
4import { createClient, OAuthStrategy } from '@wix/sdk';5import { items } from '@wix/data';6
7const myWixClient = createClient({8 modules: { items },9 auth: OAuthStrategy({10 clientId: `10c1663b-2cdf-47c5-a3ef-30c2e8543849`,11 tokens: JSON.parse(Cookies.get('session') || null),12 }),13});14
15export default function Examples() {16 const [examples, setExamples] = useState([]);17
18 async function fetchExamples() {19 const examples = await myWixClient.items20 .queryDataItems({ dataCollectionId: 'examples' })21 .ascending('orderId')22 .find();23 setExamples(examples.items);24 }25
26 useEffect(() => {27 fetchExamples();28 }, []);29
30 return (31 <div>32 {examples.map((example) => (33 <a href={example.data.slug} key={example._id}>34 <h2>35 {example.data.title} <span>-></span>36 </h2>37 <p>{example.data.description}</p>38 </a>39 ))}40 </div>41 );42}