How to Create an ads.txt Page with Cloudflare Workers
A guide on how to create an ads.txt page using Cloudflare Workers
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!
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:
- Authorized Digital Sellers: ads.txt allows publishers to publicly declare the companies authorized to sell their digital inventory. This helps prevent unauthorized reselling.
- Transparency and Trust: Advertisers can verify the authorized sellers, ensuring they are buying genuine inventory.
- 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
- Log into your Cloudflare account: Navigate to the Cloudflare dashboard.
- Navigate to Cloudflare's Dashboard: Navigate to the Cloudflare dashboard i.e., the main screen before selecting a site.
- Navigate to Workers & Pages: Click on the “Workers & pages” tab from the dashboard.
- Create a Worker: Click on the “Create a Worker” button. This will take you to the Workers editor screen.
- 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.
Step 3: Deploy the Worker
- 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.
- Select Your Site in the Cloudflare Dashboard: In the Cloudflare dashboard, select your site and enter the site's screen.
- Access Workers Routes: Under the Worker Routes tab, you will find a section for HTTP Routes.
- 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
). - 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
- Performance: Cloudflare’s global network ensures that your ads.txt file is served quickly from a location close to the requester.
- Scalability: As a serverless solution, Cloudflare Workers can handle a high volume of requests without the need to manage infrastructure.
- Simplicity: Creating and managing Workers is straightforward, requiring minimal setup and maintenance.
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.
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.
![](https://mania.africa/content/images/size/w1200/2023/12/https-3a-2f-2fsubstack-post-media-s3-amazonaws-com-2fpublic-2fimages-2f7f36fd17-5a0e-48ec-8828-0407aa3b71f1_2000x1577-jpeg-1-1.jpg)
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.