How to Create an ads.txt Page with Cloudflare Workers

A guide on how to create an ads.txt page using Cloudflare Workers

How to Create an ads.txt Page with Cloudflare Workers
Here's a guide on how to create an ads.txt page using Cloudflare Workers. Photo by Campaign Creators / Unsplash

In the world of digital advertising, transparency, and control are paramount for both publishers and advertisers. One effective tool to achieve this is the ads.txt file, which stands for Authorized Digital Sellers. This file helps ensure that your digital ad inventory is only sold through authorized sellers, mitigating the risk of fraud. This guide will walk you through the process of creating an ads.txt page using Cloudflare Workers, a powerful platform for running serverless code.

Featured: Elevate Your Website's Performance with Cloudways Cloud Hosting!

Unlock the full potential of your website with Cloudways Cloud Hosting!

Experience lightning-fast speeds, ironclad security, and seamless scalability. With our user-friendly platform, you can easily manage your applications and servers, ensuring peak performance around the clock.

Enjoy the flexibility of choosing from top cloud providers like AWS, Google Cloud, and DigitalOcean. Plus, our expert support team is available 24/7 to assist you every step of the way.

Don’t settle for average hosting—elevate your online presence today.

Cloudways Discount Code: Use the code MANIAINC to get 20% off Cloudways.

Start your free trial with Cloudways and witness the difference in performance and reliability!

Get a Cloudways Free Trial

Read our Cloudways Review for more details.

We may earn a commission when you make a purchase.

What is ads.txt?

Before diving into the technical details, let's briefly understand what ads.txt is and why it's important:

  1. Authorized Digital Sellers: ads.txt allows publishers to publicly declare the companies authorized to sell their digital inventory. This helps prevent unauthorized reselling.
  2. Transparency and Trust: Advertisers can verify the authorized sellers, ensuring they are buying genuine inventory.
  3. Industry Standard: Managed by the IAB Tech Lab, ads.txt is an industry standard supported by major advertising platforms like Google AdSense.

Setting Up Cloudflare Workers

Cloudflare Workers is a serverless computing platform that allows you to run JavaScript code on Cloudflare’s network. It’s an ideal solution for tasks like serving an ads.txt file because it’s fast, scalable, and easy to implement.

Step 1: Create a Cloudflare Worker

  1. Log into your Cloudflare account: Navigate to the Cloudflare dashboard.
  2. Navigate to Cloudflare's Dashboard: Navigate to the Cloudflare dashboard i.e., the main screen before selecting a site.
  3. Navigate to Workers & Pages: Click on the “Workers & pages” tab from the dashboard.
  4. Create a Worker: Click on the “Create a Worker” button. This will take you to the Workers editor screen.
  5. Name Your Worker: Add a descriptive name for your Worker.

Step 2: Add the Code

In the Workers editor, you will need to add the following JavaScript code. This code will serve as your ads.txt file when someone requests it. Remember to replace the lines under '#Ads.txt file for mania.africa' with entries you want in your ads.txt.

const adstxt = `
# Ads.txt file for mania.africa
google.com, pub-0000000000000000, DIRECT, f08c47fec0942fa0
google.com, pub-1111111111111111, DIRECT, f08c47fec0942fa0
google.com, pub-2222222222222222, DIRECT, f08c47fec0942fa0
# Add more lines as needed
`;

async function handleRequest(request) {
  try {
    const init = {
      'content-type': 'text/plain'
    };
    return new Response(adstxt, init);
  } catch (error) {
    return new Response(`Error: ${error.message}`, { status: 500 });
  }
}

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

Explanation of the Code:

  • adstxt Variable: This variable holds the contents of your ads.txt file. Replace the placeholder text with your actual ads.txt entries.
  • handleRequest Function: This function creates a new HTTP response with the ads.txt content and sets the content type to text/plain.
  • addEventListener: This listens for fetch events and responds with the ads.txt content when requested.
computer code on a screen | How to Create an ads.txt Page with Cloudflare Workers | Mania Guides
Deploying the code above effectively will encapsulate your ads.txt in a Cloudflare Worker. Photo by Aaron McLean / Unsplash

Step 3: Deploy the Worker

  1. Save and Deploy: After adding your code, click the “Save and Deploy” button. This will make your Worker live.

Step 4: Create a Worker Route

To ensure that the ads.txt file is served correctly when requested from your domain, you need to create a Worker route.

  1. Select Your Site in the Cloudflare Dashboard: In the Cloudflare dashboard, select your site and enter the site's screen.
  2. Access Workers Routes: Under the Worker Routes tab, you will find a section for HTTP Routes.
  3. Create a Route: Click on “Add Route”. Here, you will specify the path where your Worker will respond. Enter your domain followed by /ads.txt (e.g., https://yourdomain.com/ads.txt).
  4. Assign Worker: Assign the previously created Worker to this route.

Step 5: Testing the Worker

After setting up the Worker route, you can now test your Worker by appending /ads.txt to your domain in the browser. For example, navigate to https://yourdomain.com/ads.txt. If everything is set up correctly, you should see the contents of your ads.txt file displayed in the browser.

Benefits of Using Cloudflare Workers for ads.txt

  1. Performance: Cloudflare’s global network ensures that your ads.txt file is served quickly from a location close to the requester.
  2. Scalability: As a serverless solution, Cloudflare Workers can handle a high volume of requests without the need to manage infrastructure.
  3. Simplicity: Creating and managing Workers is straightforward, requiring minimal setup and maintenance.
A laptop with words on the screen | Creating an ads.txt page with Cloudflare Summary | Mania Africa
An ads.txt configuration via Cloudflare Workers assists you in ensuring transparency for the digital ads on your site and is a good alternative to uploading the file on your server root. Photo by Ben Kolde / Unsplash

Creating an ads.txt page with Cloudflare Summary

Creating an ads.txt page with Cloudflare Workers is a simple yet effective way to manage and serve your ads.txt file. By following the steps outlined in this guide, you can ensure that your digital ad inventory is protected and transparent, fostering trust with advertisers and maximizing your revenue potential. Whether you're a small publisher or a large media company, implementing ads.txt via Cloudflare Workers is a smart move for securing your digital advertising ecosystem.

Mania Guides - Mania Africa
All our Guides and Tutorials. We offer insight into how you can best achieve a set goal, deliberate on the most effective approaches, and guide you on how to go about carrying them out.

Read more Tech Guides here

By leveraging the power of Cloudflare Workers, you not only enhance the security and transparency of your ad inventory but also benefit from the performance and scalability of Cloudflare's network. This setup is a win-win for both publishers and advertisers, contributing to a healthier and more trustworthy digital advertising landscape.

Everything You Need to Know About Cloudflare’s Rocket Loader: A Comprehensive Guide to Boosting Site Speed and Resolving Script Conflicts
Rocket Loader is a performance optimization feature offered by Cloudflare, a leading content delivery network (CDN) provider.

Video: How to Setup your Ads.txt File Using Cloudflare Workers

For further assistance, you can refer to Cloudflare’s documentation or watch the video guide for a visual walkthrough.

Bonus: If you're using Ghost CMS and need to know how to set up Cloudflare Caching, refer to this article for the best Cloudflare settings for Ghost CMS.