

In this tutorial, we dive deep into setting up Payload NextJS with MongoDB Atlas. We'll cover everything from creating a new project to establishing access control, ensuring your application is secure and efficient.
This is the written guide for our Comprehensive Payload NextJS Setup Guide Series (Part 2). You can find the full length video here:
https://www.youtube.com/watch?v=n2f-ZfVfhCI
Setting up Payload NextJS with MongoDB Atlas may seem daunting, but it’s a straightforward process. The first step is to create your MongoDB Atlas project, which will serve as the backbone of your application. This guide will walk you through the entire setup process from start to finish, ensuring you have a solid foundation for your Payload NextJS application.
To kick things off, log in to your MongoDB Atlas account. If you don’t have one, create an account and sign in. Once you’re in, you’ll see an option to create a new project. Click on New Project.

Name your project something relevant, like Basic Setup. Once you’ve done that, click Next. You’ll have the option to add additional owners, but for simplicity, you can skip that for now. After a brief wait, your project will be created.
With your project set up, the next step is to create your database. Click on Build a Database. You’ll be presented with three options: a paid dedicated cluster, serverless, and a free M0 cluster. For most small projects, the free M0 cluster is sufficient.
Select your hosting provider—AWS is a common choice—and choose a region. I recommend Frankfurt for this example. You can keep the default cluster name as Cluster0 and click Create to initiate the process.
Once your database is created, it’s time to set up user access. Click on Database Access to manage database users. Here, you'll create a new user who will have specific access to your database.

Click on Add New Database User. You can use the default settings for the username and password, but make sure to save the password securely. This user will be essential for connecting your Payload NextJS application to MongoDB.
It’s crucial to understand that a cluster can contain multiple databases. This means that access control must be configured on a database-specific level. If you plan to use a single database, this may not be necessary, but if you have multiple projects sharing a cluster, you’ll want to ensure each project can only access its respective database.

To set up a custom role, return to the Database Access section. Click on Add New Custom Role. Name it Basic Setup and define its permissions, ensuring it has access only to your specific database.
Creating custom roles helps you manage what each user can do within your database. After defining your custom role, create a user and assign this role to it. This user will then have limited access tailored to your project's needs.
After creating the user, MongoDB will automatically restrict its permissions to the defined custom role, ensuring that it can only interact with the specified database.
To enhance security, you need to whitelist the IP addresses that will access your database. Navigate to the Network Access tab. Here, you’ll see your current IP address automatically whitelisted. If you plan to access the database from multiple locations, you can add those IP addresses as well.
Be cautious with the Allow Access from Anywhere option, as it poses significant security risks. Always limit access to specific IP addresses whenever possible.
Finally, copy your connection string from the Connect section. This string will be crucial for linking your Payload NextJS application to your MongoDB database. Remember to replace the placeholders with your custom user credentials and database name.
With your database set up and ready for action, the next step is to copy your connection string. This string is essential for linking your Payload NextJS application to your MongoDB database.

To find your connection string, navigate back to the Connect section of your database. Click on Connect, then select Drivers. You will see a code snippet that resembles the following format:
mongodb+srv://:@cluster0.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
Remember, you need to replace the placeholders with your actual username and password. In our case, it will look something like this:
mongodb+srv://basic-setup-user:@cluster0.mongodb.net/basic-setup-db?retryWrites=true&w=majority
It's critical to ensure that the database name matches the one you created earlier, otherwise you'll encounter connection errors.
Now that you have your connection string ready, it's time to initialize your Payload project. Open your terminal and run the following command:
npx create-payload-app
This command will guide you through the setup process, starting by asking for your project name. Enter basic setup and press enter.
Next, you will be prompted to choose a template. For this setup, we will start with a blank template, so simply hit enter to proceed.
When asked for your MongoDB connection string, paste the formatted string you copied earlier. This will link your Payload NextJS application to your MongoDB Atlas database.

After entering your connection string, the installation process will begin. This may take a few moments depending on your internet speed. Once it's complete, you will have a basic Payload project ready to go.
After initializing your project, there are a couple of minor adjustments to make to ensure everything runs smoothly.
These small tweaks will optimize your development environment, making it easier to work with your Payload NextJS application.
With everything set up, it's time to run your application. In your terminal, navigate to your project folder and execute:
npm run dev
This command starts your development server, and you should see a message confirming that it has successfully connected to your MongoDB Atlas server.

Once the server is running, open your browser and navigate to localhost:3000/admin. You will be greeted with the Payload admin interface where you'll be prompted to create your first user.
This admin panel is where you will manage your content and settings for the Payload NextJS application. Take a moment to explore the interface and familiarize yourself with the options available.
Congratulations! You've successfully set up your Payload NextJS application with MongoDB Atlas. You've learned how to create a database, configure access controls, initialize your project, and make necessary adjustments.
In the next steps, we will dive deeper into configuring the Payload settings, adjusting environment variables, and optimizing the Webpack configuration to suit your project's needs.
Payload NextJS is a headless CMS built on top of Next.js, allowing developers to create powerful content-driven applications with ease.
Whitelisting IP addresses enhances the security of your database by restricting access to known IPs, minimizing the risk of unauthorized access.
Yes, MongoDB Atlas supports multiple cloud providers, including AWS, Google Cloud, and Azure. You can choose the one that best fits your needs.
Double-check your connection string, ensuring that the username, password, and database name are correct. Also, verify that your IP address is whitelisted in the MongoDB Atlas settings.