Skip to main content

Command Palette

Search for a command to run...

Created my own IP to Location api using cloudflare worker, with in 1 min.

Updated
1 min read
Created my own IP to Location api using cloudflare worker, with in 1 min.
D

Developer

There are paid IP-to-location services available; no, I do not need another registration just to get the IP-to-location details; I am going to use Cloudflare for this. This will return client-side details in both languages. So for a small app, this is ok.

/**
 * @typedef {Object} Env
 */

export default {
    /**
     * @param {Request} request
     * @param {Env} env
     * @param {ExecutionContext} ctx
     * @returns {Promise<Response>}
     */
    async fetch(request, env, ctx) {
        // console.log(request);
        return Response.json({
      // request: request.cf,
      "longitude": request.cf.longitude,
      "latitude": request.cf.latitude,
      "continent": request.cf.continent,
      "country": request.cf.country,
      "city": request.cf.city,
      "timezone": request.cf.timezone,
      "colo":request.cf.colo,
      "postalCode":request.cf.postalCode,
      "region":request.cf.postalCode,
      "regionCode":request.cf.regionCode,
      "asOrganization":request.cf.asOrganization
    });
    },
};

Just paste in your cloudflare worker code and that is it You have a working IP endpoint, which gives you lang and lat. Now you can also add requests inside this worker, so it will also make other requests to another API to use this lang and lat data to fetch something, for example, weather data.