Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript

Posted by Dmitry

Updated on

javascript cloudflare geolocation workers

If you ever needed to detect a visitor's country and city by IP address you probably stumbled upon services such us IPInfo. Although they're good, it's overkill and too expensive. IPInfo went too wild with its pricing.

A good free customizable alternative can be Cloudflare Workers. With the free plan, you can make up to 100k requests per day. And if you need more — paid plan is just $5/mo.

Cloudflare Workers Dashboard

Login to your Cloudflare account and go to Workers from the sidebar menu.

Next, click «Create a Service».

Cloudflare Create New Worker/Service

Enter desirable service name, or leave it predefined. I prefer these randomized names, like snowy-barcode-1433. Choose «HTTP Handler» as a starter.

Hit «Create service».

Cloudflare Worker/Service Screen

Now, click «Quick Edit» on the right side of the screen.

In the opened screen, replace default code with the code below.

Finally, hit «Save and Deploy».

Now, when you open worker URL(https://empty-bird-5317.your-company-name.workers.dev/) in the browser you'll see response below.

{
  "city": "Tbilisi",
  "country": "GE",
  "ip_addr": "your-ip-addr"
}
Keep in mind, that this endpoint is opened to the world. Remove Access-Control-Allow-Origin if you want to remove CORS support. You can find more about this in the official documentation.

How To Access Cloudflare Worker From The Browser

Postface

It goes without saying that you can now customize this endpoint to return more parameters. Take a look at the official documentation for more options. Don't forget to explore Cloudflare Workers Request.

The Latest

recv() failed (104: Connection reset by peer) while reading response header from upstream

python uwsgi error snippet

JavaScript Document Onload + TypeScript version

javascript DOM snippet basics typescript
🗃 JavaScript Basics

Universal code-snippet to know when HTML is ready for modern browsers.

How to Create Jinja2 Filters in Flask

python flask jinja2 snippet

In this post, I'll talk about filters. Jinja2 has a list of built-in filters, and Flask leverages them.

How to Upload Files To Bunnynet Storage

python bunny_net storage snippet

Bunny.net is a well-known CDN and storage among developers. Here is my python snippet on how to upload a file to the storage.

How to Copy Text to Clipboard With Javascript

简体中文 javascript DOM browser

Here is a short snippet on how to copy text to the clipboard with Javascript. Your visitors will thank you. Most likely not, but here we are.

Flask Boilerplate and Your Guide to Flask in 2023. With SQLAlchemy.

boilerplate open source flask

Flask-Backbone is my take to create an initial structure and a set of rules, so most of my flask projects are easy to support.

How to Import CSV Files to PostgreSQL with Pgfutter

csv postgresql

Sometimes I need to upload large CSV files to PostgreSQL. CSV file might have hundreds of columns, that's why i want a tool that can do some magic for me.

How to Upload Files to DigitalOcean Spaces with Python

digitalocean python

Snippet on how to upload files to DigitalOcean Spaces with boto3 and python.

The Definitive Guide To Sitemaps With Python

open source python

Sitemaps are important. Especially for big websites. It is always a good idea to develop your website with SEO in mind. Unfortunately, most developers ignore this part.

A Short Guide To The Chinese Coordinate System

Have your ever searched google maps china offset? Most people who was in China yes. Here is my story behind this question.