Was this page helpful?

Create Contentful App

Apps allow you to embed single page applications inside the Contentful web app and extend the Contentful experience. To make the process of creating a new app simpler, the create-contentful-app CLI supports creating an app template, creating an AppDefinition, and gives instructions on how to install the app into a space environment.

This article explains how to create and develop an app from scratch using create-contentful-app.

Prerequisites

In order to use this command line tool, you'll need Node.js version 14 or newer installed on your machine. You will also need a Contentful account with an organization management role. Organization management roles are owners, admins or developers.

Create a new app

To get started, run create-contentful-app in your command line:

npx create-contentful-app my-first-app

This command generates your app project and installs all required dependencies.

public
└── index.html
src
├── components
│   └── LocalhostWarning.tsx
├── locations
│   ├── ConfigScreen.spec.tsx
│   ├── ConfigScreen.tsx
│   ├── Dialog.spec.tsx
│   ├── Dialog.tsx
│   ├── EntryEditor.spec.tsx
│   ├── EntryEditor.tsx
│   ├── Field.spec.tsx
│   ├── Field.tsx
│   ├── Home.spec.tsx
│   ├── Home.tsx
│   ├── Page.spec.tsx
│   ├── Page.tsx
│   ├── Sidebar.spec.tsx
│   └── Sidebar.tsx
├── App.tsx
├── index.tsx
├── react-app-env.d.ts
└── setupTests.ts
.gitignore
README.md
package.json
package-lock.json

When the command finishes it will have created a new directory called my-first-app.

Note: The created package.json contains all packages needed for creating your app together with a field called homepage referring to the local directory. What this does is, all the links in your build will be relative to the location of the index.html. To find out more you can read here.

Options

By default, create-contentful-app creates a Contentful app using TypeScript. If you prefer to build your app using vanilla JavaScript, instead run npx create-contentful-app -js my-first-app.

To create your new app using one of Contentful's public examples, use the --example option followed by the example name.

To use Yarn instead of npm, you can pass the --yarn flag.

To learn more about what create-contentful-app is capable of, run:

npx create-contentful-app --help

Creating an AppDefinition

You need to create an AppDefinition for your app in order to use it on Contentful. This can either be done through the Contentful web application or through create-contentful-app. To do this using create-contentful-app, open your project folder and configure your app:

cd my-first-app
npm run create-app-definition

Running the create-app-definition command will walk you through the process of configuring your app, and creating an AppDefinition. If necessary the command will also walk you through the process of authenticating with Contentful. The configuration created in this step can easily be changed by following this link.

Local development

Once the configuration is complete, run the app:

npm start

This command starts the development server and will walk you through installing your app into a Contentful space environment. The extension will automatically reload if you make changes to the code.

Note: As Contentful runs in an HTTPS environment, temporarily disable the security checks in the browser. For example, enable "Load unsafe scripts" in Chrome.

Deploy with Contentful

Building an app

Before deploying your app you will need to build it. Do this by running npm run build, this will create a build folder.

Uploading an app

Now you can upload your build to Contentful with the following command:

npm run upload

The command prompts you for some required options to upload the build folder and to create a bundle. Alternatively, if you want to run it in a CI pipeline, you can run it with the required options as arguments:

npm run upload --ci \
     --organization-id some-org-id \
     --definition-id some-app-def-id \
     --token your-contentful-access-token

Note: You can also pass all arguments in interactive mode without the --ci option to skip the prompts. Passing --ci just makes the command fail when the mandatory arguments are missing.

If you have already set all options as environment variables, you can also run the following command, which automatically takes your defined environment variables as arguments:

npm run upload-ci

Options:

Argument Description Environment Variable
--organization-id The ID of your organization CONTENTFUL_ORG_ID
--definition-id The ID of the app to which to add the bundle CONTENTFUL_APP_DEF_ID
--token A personal access token CONTENTFUL_ACCESS_TOKEN
--skip-activation (optional) Boolean flag to skip the automatic activation of the bundle -

If the upload was successful, the uploaded bundle will be activated right away and deployed to your defined AppDefinition. If you do not want to activate it automatically, you can skip it with the --skip-activation option in interactive and non-interactive mode.

Activating a bundle

To make your app serve a specific bundle, you can activate it manually by running the activate command:

npm run activate

When you run this command, you will be guided through some questions to get all the information to activate your bundle. Similar to upload, you can also run this command with passing all options as arguments:

npm run activate --ci \
     --bundle-id some-bundle-id \
     --organization-id some-org-id \
     --definition-id some-app-def-id \
     --token your-contentful-access-token

Note: You can also pass all arguments in interactive mode without the --ci option to skip the prompts. Passing --ci just makes the command fail when the mandatory arguments are missing.

Options:

Argument Description Environment Variable
--bundle-id The ID of the bundle you want to activate -
--organization-id The ID of your organization CONTENTFUL_ORG_ID
--definition-id The ID of the app to which to add the bundle CONTENTFUL_APP_DEF_ID
--token A personal access token CONTENTFUL_ACCESS_TOKEN

Programmatic app management

To manage an app programmatically, refer the Content Management SDKs.

Tracking

We gather depersonalized usage data of our CLI tools in order to improve experience. If you do not want your data to be gathered, you can opt out by providing an env variable DISABLE_ANALYTICS set to any value:

DISABLE_ANALYTICS=true npx create-contentful-app

Next steps

Not what you’re looking for? Try our FAQ.