Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Using Cloudflare Workers and API to block access to the pages filtered if the IP belongs to a blacklisted IP address of the service
addEventListener('fetch', event => {
async function fetchAndCheckOrigin(req) {
try {
const body = await req.body;
const ip = req.headers.get('cf-connecting-ip');
const apilityio = await fetch('' + ip + '?token=APILITY_IO_API_KEY');
const status = await apilityio.status;
if (status == 200) {
// The IP has been found in any blacklist
return new Response('Sorry, you cannot access this site from this IP: ' + ip,
{ status: 403, statusText: 'Forbidden' })
if (status == 429) {
// Quota Exceeded. Too many requests.
return new Response('Your quota has run out. Please change your plan to support more traffic.',
{ status: 429, statusText: 'Too many requests.' })
return await fetch(req)
} catch (err) {
return new Response('Internal Error')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.