All Blog Articles

Getting Started with Payload NextJS: A Comprehensive Setup Guide

Media
Sandro WegmannMarch 5, 2025
Payload CMS Basic Setup Part 1: Introduction

Welcome to the first part of our tutorial series on Payload NextJS! In this guide, we will walk you through the essential steps to set up Payload CMS effectively, ensuring you have a solid foundation for your project. From initializing your MongoDB Atlas project to deploying on Railway, we've got you covered.

This is the written guide for our Comprehensive Payload NextJS Setup Guide Series (Part 1). You can find the full length video here:

https://www.youtube.com/watch?v=jCxmA4usUAw


Table of Contents

Introduction to Payload CMS

Payload CMS is a headless content management system that allows developers to create and manage content seamlessly. It stands out due to its flexibility and developer-friendly features. With Payload, you can build custom content structures, use modern frameworks like NextJS, and manage your content through a powerful admin interface.

What makes Payload unique is its focus on developer experience. It uses a schema-based approach, allowing you to define content types with rich fields and relationships. This means you can create complex data structures that fit your application's needs without compromise.

Why Choose Payload CMS?

Choosing Payload CMS offers numerous benefits:

  • Flexibility: Easily customize your content models and adapt them as your project evolves.
  • Performance: Built with efficiency in mind, Payload is optimized for speed, making it suitable for high-traffic applications.
  • Rich API: Interact with your content through a GraphQL API, ensuring a smooth integration with your frontend.
  • Developer Experience: The intuitive admin panel and extensive documentation empower developers to work efficiently.
  • Community Support: With an active community, finding solutions and resources is straightforward.

Setting Up MongoDB Atlas

To kick off your Payload project, the first step is to set up a MongoDB Atlas account. This cloud database service offers a free tier, which is perfect for small projects. Here’s how to get started:

  1. Visit the MongoDB Atlas website and create an account.
  2. Once logged in, create a new project.
  3. Select "Build a Cluster" and choose the free tier option.
  4. Configure your cluster settings according to your preferences, then click "Create Cluster."
  5. After your cluster is set up, navigate to the "Database Access" section to create a new database user with read and write permissions.
  6. Finally, go to "Network Access" to allow access from your IP address.

Once these configurations are complete, you’ll have a MongoDB Atlas cluster ready to use.

Setting-Up-MongoDB-Atlas.png

Initializing Your Payload Project

Now that you have your MongoDB Atlas cluster set up, it’s time to initialize your Payload project. This is done using the command line interface.

  1. Open your terminal and navigate to the directory where you want your project to reside.
  2. Run the command npx create-payload-app your-project-name. This command creates a boilerplate Payload project.
  3. Once the installation is complete, navigate into your project directory with cd your-project-name.
  4. Start your project by running npm run dev.

This will launch your Payload CMS application, and you can access it via your browser.

Initializing-Your-Payload-Project.png

Configuring Environment Variables

Configuring environment variables is crucial for your application to communicate with MongoDB Atlas securely. Here’s how to set them up:

  1. Create a new file in your project root named .env.
  2. Add your MongoDB connection string to this file. It typically looks like this:

MONGODB_URI=mongodb+srv://:@cluster.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

  1. Replace and with your actual MongoDB credentials.
  2. Save the file and ensure it is not tracked by version control by adding it to your .gitignore.

These variables will help Payload connect to your database securely, ensuring your credentials are not exposed.


Adjusting Payload and Webpack Configurations

To optimize your Payload application, you need to adjust both the Payload and Webpack configurations. This ensures that your application runs smoothly and avoids common pitfalls.

Adjusting Payload Configuration

Open the payload.config.ts file in your project. Here are some adjustments to consider:

  • Set up CORS if your frontend and backend are on different domains.
  • Configure authentication cookies for seamless login experiences.

Adjusting Webpack Configuration

Next, modify your Webpack configuration. This is particularly important for handling server-only modules:

  • Prevent Webpack from bundling server-only modules, which can lead to issues during runtime.
  • Ensure that your configuration is optimized for production builds.

These adjustments will improve the performance and security of your Payload CMS application.


Setting Up S3 Storage for File Uploads

By default, Payload saves uploaded files to the local hard drive, which isn’t ideal for multi-environment setups. Instead, using S3 storage is a better approach. Here’s how to set it up:

  1. Choose an S3 provider, such as AWS, and create an S3 bucket.
  2. Configure the bucket permissions to allow your application to upload files.
  3. In your Payload project, install the necessary packages for S3 integration:

npm install aws-sdk multer-s3

  1. Update your Payload configuration to include the S3 storage configuration.

This setup ensures that your uploaded files are stored externally, making it easier to manage across different environments.


Implementing a Transactional Email Service

Once your Payload NextJS project is up and running, setting up a transactional email service is essential for effective communication. For testing purposes, Payload offers a service called Ethereal Email. However, this can be cumbersome since it generates new credentials every time the server restarts.

To streamline this process, we will implement a workaround. For production emails, we will use Brevo, formerly known as Sendinblue. Brevo is a GDPR-compliant email service, making it a solid choice for European projects.

Steps to Integrate Brevo

  1. Sign up for a Brevo account and create an API key.
  2. 2. In your Payload project, install the necessary email package:

npm install nodemailer

3. Configure your email settings in the payload.config.ts file, like so:


import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
    host: 'smtp.brevo.com',
    port: 587,
    auth: {
        user: '',
        pass: '',
    },
}); 

4. Define your email templates and send emails using the transporter you configured.

This setup allows you to send transactional emails seamlessly, enhancing user experience.


Adding Tailwind CSS to Your Project

To enhance the aesthetics of your admin panel, integrating Tailwind CSS is a game-changer. Tailwind allows for rapid styling with utility-first classes, making it easy to customize without losing the default styles of Payload.

Steps to Integrate Tailwind CSS

  1. Install Tailwind CSS via npm:

npm install tailwindcss postcss autoprefixer

  1. Generate your configuration files:

npx tailwindcss init -p

  1. In your tailwind.config.js, specify the paths to your template files:
module.exports = {
    content: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
    theme: {
        extend: {},
    },
    plugins: [],
};
  1. Add Tailwind's base styles to your main CSS file:

@tailwind base;
@tailwind components;
@tailwind utilities;

Now Tailwind CSS is ready to use, allowing you to customize the admin panel effortlessly.


Deploying Your Project on Railway

Deploying your Payload NextJS project can be a daunting task, but Railway simplifies the process significantly. With Railway, you can connect your GitHub repository and deploy your project quickly without the hassle of managing servers.

Steps to Deploy on Railway

  1. Create a Railway account and log in.
  2. Click on "New Project" and select "Deploy from GitHub."
  3. Authorize Railway to access your GitHub account.
  4. Select the repository containing your Payload project.
  5. Add any necessary environment variables directly in the Railway dashboard.
  6. Click "Deploy" and watch as Railway handles the rest!

Once deployed, Railway automatically redeploys your project every time you push changes to your GitHub repository, making it incredibly efficient.


Cost Benefits of Using Railway

One of the standout features of Railway is its cost efficiency. Unlike traditional servers where you pay a flat rate, Railway only charges for the resources you use. This is particularly beneficial for small to medium-sized projects.

Understanding Railway's Pricing Model

  • Resource-Based Pricing: You pay for CPU, memory, and storage based on your actual usage.
  • Free Credit: Railway provides a $5 credit each month, perfect for small projects or development environments.
  • Cost Estimates: Most average Payload projects cost around $2 to $3 a month, allowing you to run multiple projects affordably.

This pricing model ensures that you can scale your project without the fear of skyrocketing costs.


Conclusion and Next Steps

Congratulations! You’ve successfully set up your Payload NextJS project with essential functionalities like transactional emails and Tailwind CSS integration. The deployment on Railway further streamlines your workflow.

As you move forward, consider exploring advanced features of Payload CMS. Integrate more complex content types or enhance your admin panel with custom functionality. The possibilities are endless!


FAQs about Payload NextJS

What is Payload NextJS?

Payload NextJS is a headless content management system that seamlessly integrates with NextJS, allowing for flexible content management and a robust developer experience.

How do I set up a database for Payload?

You can easily set up a MongoDB Atlas database, which is free for small projects. Follow the configuration steps outlined earlier to connect Payload to your database.

Can I use other email services with Payload?

Yes, while we recommend Brevo for its GDPR compliance, you can integrate other email services like SendGrid or Mailgun as well.

Is Railway the only deployment option?

No, you can deploy your Payload NextJS project on various platforms, but Railway simplifies the process significantly.