Develop, edit, and deploy websites entirely in the cloud with CodeSandbox, Contentful, and Netlify
![In this tutorial we combine the powers of Contentful, CodeSandbox and Netlify, building and deploying a full-stack application entirely in the web browser.](https://images.ctfassets.net/fo9twyrwpveg/4ScuFnmzqYgaBhv696ccJ5/e2566fd2c217a7176205333bc2c77f06/2022-11_Contentful_Netlify_CodeSandbox.png?fm=jpg&q=90&w=1536)
The emergence of cloud computing platforms has revolutionized how data is managed, processed, and shared. The cloud computing market is expanding rapidly and replacing the traditional computing methods that rely on physical hardware.
Cloud computing entails using a remote server (or a network of them) to store, manage, or process data, making it accessible from anywhere.
Today, we’ll look at combining Contentful, CodeSandbox, and Netlify. In this tutorial, you’ll combine these tools to build and deploy a full-stack application, entirely in the browser, without having to set up a server, database, or local development environment.
Ready? Then let's show you how to build a movie collection using your Contentful space. You can find the full project code on GitHub.
What is CodeSandbox?
CodeSandbox provides the same complete developer experience that you would find in most advanced IDE software. The difference is that it provides all of this right inside your browser. The CodeSandbox environment includes syntax highlighting, a debugger, a source code editor, code completion, real-time compilation, and collaboration.
Additionally, to speed up development, CodeSandbox provides starter templates for over 50 projects, including popular ones like React, Vue, Angular, and Node.js, as well as static site generators like Next.js and Nuxt.js. Each project includes the relevant tools and library dependencies.
What is Netlify?
Netlify is a hosting platform that provides everything you need to deploy your modern web apps without thinking about servers and DevOps. Netlify has a rich set of features, including free hosting, Git integration (including easier rollbacks), and deploy previews, perfect for frontend web apps and static sites. Netlify also integrates very well with the tools used in this tutorial.
Starting the project with Contentful
For simplicity, each movie will have just four properties: a title, plot, featured image, and IMDb link. You can use any assets you’d like to build this project, but here is a link to download the assets used in our demonstration, including mock movie posters made with freely available images.
To get started, you’ll need a Contentful account. When you first sign in, a new space — named Blank — should be created for you automatically. If not, create a new space and give it a name.
Now you’re ready to create your content model and types.
1. Create a content type
In Contentful, the content model determines your content's structure and organization. Within it, you have individual content types. To get started, you need to create your first content type.
From inside your new space, click Content Model on top of the page, then click Create a content type.
![First Type](https://images.ctfassets.net/fo9twyrwpveg/35rIvtTXrPngAf386W52q9/670b9958df1308a84ce1f56b7a929f5a/unnamed__31_.png)
![Create Content Type](https://images.ctfassets.net/fo9twyrwpveg/7vBi93Kb7rzwr3jpkLmC9y/9c4619e0f45ba6f08f2078ba4c74b1ff/unnamed__32_.png)
The form that appears enables you to specify the kind of content type you want to create. For this tutorial, you’ll create the Movie
type. This type will have the following fields:
Title
— Represents the title of a movie. It uses the Text type.Plot
— Represents the movie plot. It uses the Text type.FeaturedImage
— Represents the movie image. It uses the Media type.IMDb
— Represents the IMDb URL of the movie. It uses the Text type.
To add a field to your Movie
type, click on the Add field button on the right and choose from the types provided in the menu:
![Add New Field](https://images.ctfassets.net/fo9twyrwpveg/2FPFv6ARdD5dLuMI1VUA6P/8051f54d40c8d7827abae2dd8a54f199/unnamed__34_.png)
Once you're done with all fields, your content type should look like this:
![Movie List](https://images.ctfassets.net/fo9twyrwpveg/45wrbuce1jHZi25ob8ba8T/952f6789f97d5adb3ac2bfb2a7ec6e52/unnamed__35_.png)
Click Save to save your content type as is.
2. Add content
So far, you’ve created the Movie content type and added four fields to it. Now it’s time to add some movies.
While still within your space, click on Content to create your first Movie entry:
![Oblivion](https://images.ctfassets.net/fo9twyrwpveg/01D67h7ys8tCvNVAamWPCF/46dcdd3250142320c081444502fa7083/unnamed__36_.png)
Here, you are adding the title, plot, featured image, and IMDb link of the first movie, Oblivion. Click Publish to save the entry after adding data to all the fields.
This tutorial features four entries for your movie collection. However, you can add as many as you wish.
![Add Movie](https://images.ctfassets.net/fo9twyrwpveg/7agnWIs4eItmQDB6h6CmMS/dfcf16a0774eb20a7e36f5dc96f984d8/unnamed__37_.png)
Now that you’ve finished creating your content on Contentful, you’re ready to create a React app to display these movies to users.
Start building with CodeSandbox
So far, you’ve used Contentful to create your site content without having to set up a database locally. Now, you’ll use CodeSandbox to build an app that displays the content.
1. Create a React project on CodeSandbox
To get started, sign up for a CodeSandbox account, which enables you to save your project anytime.
After signing in, create a new React sandbox. CodeSandbox will automatically scaffold a React app for you. Then, all you’ll have to do is modify the application by adding JSX and JavaScript.
2. Create client connection
Before you get started with defining the UI, you first need to install the Contentful JavaScript Client Library and instantiate a client connection to enable you to fetch the movies from Contentful into your app.
To install Contentful as a project dependency in your React sandbox, search for “contentful” and add it under Dependencies:
![Dependencies](https://images.ctfassets.net/fo9twyrwpveg/35T28AcSXkWggVnW41VlN4/538ec48b66574dc7edd22876a8d3c553/unnamed__38_.png)
Next, create a file inside your src
folder and name it client.js
. Here, you’ll use the contentful
library to create an authenticated client connection.
From inside client.js
, import contentful
:
Then, create and export a client connection instance by calling createClient()
and passing it your space id and access token:
Both space
and accessToken
values should be retrieved from your Contentful account. For the access tokens, use the Content Delivery API token.
To get these tokens, navigate to the Settings > API Keys page in your Contentful account, then click Add API Key to create the new access tokens you need.
![Name](https://images.ctfassets.net/fo9twyrwpveg/5JLgMMsIA1nm5GJYRlTCpy/aca692d05944ceaa3f1e76e181bbdbaf/unnamed__39_.png)
Click Save to activate the tokens.
Please note that API keys are meant to be private. They should never be exposed in the source code. Instead, create an .env
file and pass/access the keys as environmental variables.
3. Show the movies data
You’ve now created the API access tokens and used them to create and export an authenticated client connection. Next, you’ll use this client instance to fetch the movies and show them inside your components.
First, replace the content of your App.js
file with the following imports:
(Ignore all errors you receive for now; they’ll disappear by the time you’re done.)
Above, you imported the following code:
client
— The client connection, created using the access tokens.useState
anduseEffect
— React hooks for managing component state and executing lifecycle tasks, respectively.Movies — A container component responsible for displaying the movies. You’ll make this component later on.
Next, inside your App component, initialize the movies
state to an empty array and define its setter method, setMovies
, for updating the state once the data is fetched.
Next, call useEffect
and pass a callback function to it. In this callback, you’ll call getEntries()
on your client
to get all movie entries from your Contentful space.
Because it returns a promise, you chain then()
and catch()
to handle success and error cases. In the case of success, you simply call setMovies
and update the state with the movies
data. Otherwise, you log the error to the console.
Now that you have the movies stored in App’s state, you’ll simply pass them to <Movies />
, which is responsible for displaying them:
So, <Movies>
will render <Movie>
for each movie in the list. Now, you can create these components.
Inside your src/
directory, create a new folder and name it components
. Then, inside components, create two new files: Movies.js
and Movie.js
.
Add the following code inside Movies.js
:
Recall that in your <App>
component, you passed the movies retrieved from the API call to the <Movies />
component as props.
Then, in the component, you loop through the movie
list. For each movie, you return the currently unfinished <Movie />
component, passing the current movie to it.
Now, inside Movie.js
, use the following code:
Since the actual data is contained in the movie.fields
object, you used the destructuring syntax to get all field values in one line of code. Then in the component, you defined the markup to show the title, plot, image, and anchor link.
To style the site, include the following style declarations in public/index.html
, inside the <head>
element:
At this point, all errors should disappear from the page, and you should get the following result.
![Her](https://images.ctfassets.net/fo9twyrwpveg/4xTGBuf9dLMPfWUDhhR3Y2/f74275774e609d198b01358d8cae2c20/unnamed__40_.png)
Next, you’ll export the source code to GitHub to prepare your app for deployment. There’s no need to do this manually, as CodeSandbox enables you to export directly to GitHub.
To do this, click the GitHub icon on the left sidebar and authenticate your GitHub account, then create a new GitHub repo. This repo will be synced with your sandbox.
Deploy the movie app to Netlify
Now we're ready to deploy our movie app. You have several options for how to proceed here.
1. Deploy from CodeSandbox
Since you’re developing on CodeSandbox, it’ll take less time to deploy your React app to Netlify directly from here.
Click the Deployments (rocket) icon on the left sidebar, then click Deploy to Netlify. This will build your production bundle and upload it to Netlify.
![Netlify Upload](https://images.ctfassets.net/fo9twyrwpveg/6hLsT6b60BdasVRqzcJx6f/ec47025678697b97dd020537d473d24f/unnamed__41_.png)
You don’t need a Netlify account to deploy with CodeSandbox. Once the build process completes, click on Open Netlify Site to view the live version.
2. Deploy with GitHub (or your preferred Git provider)
Another way to deploy your React app on Netlify is via Github.
Sign in to your Netlify account, then from your dashboard, click Import from Git. Choose your Git provider (in our example, GitHub) and connect your account.
![Git repository](https://images.ctfassets.net/fo9twyrwpveg/5OytVZ5o2RIppKBNzZlk42/eda36bf3b89cd3cf6b303e6b8cbe17f4/unnamed__42_.png)
Next, search for the repository you want to upload and click on it. This should be the app that we exported from CodeSandbox.
You’ll be directed to the site settings page. Scroll to the bottom and click Deploy site.
Netlify will build your application for production, and after a few minutes, will notify you that the site is live. It will also provide a link to the site.
Wrapping up
Thanks to the tools used in this tutorial, building and deploying websites has never been easier.
These tools eliminate the challenging and time-consuming parts of web development like setting up a server, database, and dev environment and managing deployments. This enables you to focus on creating a customized and attractive frontend for your content.
This is only one of the many projects you can create with Contentful. Check out our quick start tutorials, developer showcase, or sign up for a free account today to build more.