This directory holds the code for the Localization with Contentful Tutorial.
You can either:
- install and use it as a Medusa application;
- or copy its source files into an existing Medusa application.
- Node.js v20+
- Git CLI
- PostgreSQL
- Contentful account with tokens for Management and Delivery APIs, space ID, and optionally a webhook secret.
- Clone the repository and change to the
localization-contentfuldirectory:
git clone https://github.com/medusajs/examples.git
cd examples/localization-contentful2. Rename the .env.template file to .env.
3. If necessary, change the PostgreSQL username, password, and host in the DATABASE_URL environment variable.
4. Set the Contentful environment variables:
CONTENTFUL_MANAGEMNT_ACCESS_TOKEN=
CONTENTFUL_DELIVERY_TOKEN=
CONTENTFUL_SPACE_ID=
CONTENTFUL_ENVIRONMENT=master
CONTENTFUL_WEBHOOK_SECRET= #optionalWhere:
CONTENTFUL_MANAGEMNT_ACCESS_TOKENis the token for the Management API.CONTENTFUL_DELIVERY_TOKENis the token for the Delivery API.CONTENTFUL_SPACE_IDis the ID of the Contentful space.CONTENTFUL_ENVIRONMENTis the environment to use, which ismastertypically.CONTENTFUL_WEBHOOK_SECRETis the secret used to verify webhooks. This is optional.
Learn more about retrieving these variables in the tutorial
5. If the default locale in your Contentful space is different than en-US, change it in medusa-config.ts.
6. Install dependencies:
yarn # or npm install7. Setup and seed the database:
npx medusa db:setup
yarn seed # or npm run seed8. Start the Medusa application:
yarn dev # or npm run devTo test it out, go to the Test it Out section.
If you have an existing Medusa application, copy the following directories and files into your project:
src/adminsrc/apisrc/modules/contentfulsrc/subscriberssrc/workflows
Then, add the Contentful Module to medusa-config.ts:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "./src/modules/contentful",
options: {
management_access_token: process.env.CONTENTFUL_MANAGEMNT_ACCESS_TOKEN,
delivery_token: process.env.CONTENTFUL_DELIVERY_TOKEN,
space_id: process.env.CONTENTFUL_SPACE_ID,
environment: process.env.CONTENTFUL_ENVIRONMENT,
default_locale: "en-US", // change if that's not the default locale
webhook_secret: process.env.CONTENTFUL_WEBHOOK_SECRET,
}
}
],
})Next, add the following environment variables:
CONTENTFUL_MANAGEMNT_ACCESS_TOKEN=
CONTENTFUL_DELIVERY_TOKEN=
CONTENTFUL_SPACE_ID=
CONTENTFUL_ENVIRONMENT=master
CONTENTFUL_WEBHOOK_SECRET= #optionalWhere:
CONTENTFUL_MANAGEMNT_ACCESS_TOKENis the token for the Management API.CONTENTFUL_DELIVERY_TOKENis the token for the Delivery API.CONTENTFUL_SPACE_IDis the ID of the Contentful space.CONTENTFUL_ENVIRONMENTis the environment to use, which ismastertypically.CONTENTFUL_WEBHOOK_SECRETis the secret used to verify webhooks. This is optional.
Learn more about retrieving these variables in the tutorial
After that, install the necessary Contentful packages:
yarn add @contentful/node-apps-toolkit contentful contentful-management # or npm install @contentful/node-apps-toolkit contentful contentful-managementThis guide was implemented with
@contentful/node-apps-toolkit@^3.13.0,contentful@^11.5.13, andcontentful-management@^11.52.1.
You can validate that everything is working successfully by running the Medusa application:
yarn dev # or npm run devThen, go to localhost:9000/app and log in. If you don't have user credentials, create them with the CLI tool.
After you log in, you'll find a "Contentful" sidebar item in the admin. You can click on it, then click the button in the page to sync products into Contentful.
- Medusa Documentatin
- Contentful Documentation
- OpenAPI Spec file: Can be imported into tools like Postman to view and send requests to this project's API routes.