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.

Getting started

Watch our overview video on how to use the create-contentful-app CLI

Prerequisites

In order to use this command line tool, you'll need Node.Js version 8.10 or newer, and NPM version 5.6 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, initialize an app template:

npx @contentful/create-contentful-app init my-first-app

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

src
├── components
│   ├── ConfigScreen.spec.tsx
│   ├── ConfigScreen.tsx
│   ├── Dialog.spec.tsx
│   ├── Dialog.tsx
│   ├── EntryEditor.spec.tsx
│   ├── EntryEditor.tsx
│   ├── Field.spec.tsx
│   ├── Field.tsx
│   ├── LocalhostWarning.tsx
│   ├── Page.spec.tsx
│   ├── Page.tsx
│   ├── Sidebar.spec.tsx
│   └── Sidebar.tsx
├── index.css
├── index.tsx
├── react-app-env.d.ts
└── setupTests.ts

This step may take a while. 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

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
npx @contentful/app-scripts 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

Uploading an app

If you want to deploy your app after the build folder has been successfully created by running npm run build, you can upload and run it with 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-conentful-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-conentful-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.

Next steps

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