Blog / API / Can I Get IP Geolocation Data Directly Inside Cursor and Claude Code?

Can I Get IP Geolocation Data Directly Inside Cursor and Claude Code?

Ask Cursor where 134.201.250.155 resolves to and it will answer. That answer comes from training data, not from the network. Your coding agent cannot look up an IP address unless you give it something to…

Can I Get IP Geolocation Data Directly Inside Cursor and Claude Code?

Ask Cursor where 134.201.250.155 resolves to and it will answer. That answer comes from training data, not from the network.

Your coding agent cannot look up an IP address unless you give it something to call. That takes one API key, one script, and one instruction file.

The gap shows up in the code it writes for you. Middleware that hardcodes a country, a currency switch built on an assumption, a test fixture with coordinates that land in the wrong hemisphere.

Can Cursor and Claude Code access live IP geolocation data?

Yes, once you hand them an endpoint they are allowed to hit. Neither editor ships with an IP database. Both can run terminal commands, and both read a project file that tells them which command to run.

So the setup is the same on both sides. Put a key in the environment, add a script the agent can execute, and write two lines telling it to stop guessing.

IPstack covers the data. One call returns continent, country, region, city, ZIP, latitude, and longitude for an IPv4 or IPv6 address, with time zone, currency, connection, and security data available on top.

Get your key first

Create an APILayer account and copy your IPstack access key from the dashboard. The free plan includes 100 requests, which is enough to wire this up and prove it works.

IPstack is part of the APILayer suite, so one account gives you one API key, one dashboard, and one invoice across every API on it.

Put the key in your shell, never in a source file:

export IPSTACK_KEY=“your_access_key”

The call your agent will make

Every lookup is a single GET request:

curl “https://api.ipstack.com/134.201.250.155?access_key=$IPSTACK_KEY

The path segment is the IP you want. Everything else is a query parameter.

Add fields to trim the payload. A full response nests location, time zone, currency, and connection objects, and all of it lands in your agent’s context window:

curl “https://api.ipstack.com/134.201.250.155?access_key=$IPSTACK_KEY&fields=ip,country_code,country_name,region_name,city,latitude,longitude”

That returns the seven fields you asked for and nothing else:

{
  “ip”: “134.201.250.155”,
  “country_code”: “US”,
  “country_name”: “United States”,
  “region_name”: “California”,
  “city”: “Los Angeles”,
  “latitude”: 34.0655,
  “longitude”: -118.2405
}

There is also a check endpoint that resolves whatever address the request came from, which is handy when the agent is testing against your own machine:

curl “https://api.ipstack.com/check?access_key=$IPSTACK_KEY&fields=ip,city,country_name”

Full parameter reference lives in the IPstack documentation.

How do I add the IPstack API to Cursor?

Two files. A script the agent runs, and a rule telling it to run the script.

Start with scripts/geo.py:

import json, os, sys, urllib.parse, urllib.request

KEY = os.environ.get(“IPSTACK_KEY”)
if not KEY:
    raise SystemExit(“IPSTACK_KEY is not set”)

FIELDS = “ip,country_code,country_name,region_name,city,latitude,longitude”

def lookup(ip):
    query = urllib.parse.urlencode({“access_key”: KEY, “fields”: FIELDS})
    with urllib.request.urlopen(f”https://api.ipstack.com/{ip}?{query}) as response:
        data = json.load(response)
    if data.get(“success”) is False:
        raise SystemExit(f”ipstack error {data[‘error’][‘code’]}: {data[‘error’][‘info’]})
    return data

print(json.dumps(lookup(sys.argv[1]), indent=2))

Then .cursor/rules/ipstack.mdc. Cursor only picks up project rules from that folder when they use the .mdc extension and carry frontmatter, so a plain .md file there does nothing:


description: Resolve IP addresses with IPstack instead of guessing
alwaysApply: true


Never invent geolocation values for an IP address.
Run `python3 scripts/geo.py <ip>` and use the JSON it returns.
The key is in IPSTACK_KEY. Do not print it or write it to a file.

If you would rather skip the frontmatter, Cursor also reads an AGENTS.md file in the project root. Same instruction, plain markdown, no metadata.

Now ask the agent where an address resolves to. It runs the script and answers from the response.

The same setup in Claude Code

Claude Code loads CLAUDE.md from your project root at the start of every session. The instruction goes there:

## IP geolocation

Never guess a location for an IP address. Run this instead:

`python3 scripts/geo.py <ip>`

The key is in IPSTACK_KEY. Never echo it or commit it.

Export IPSTACK_KEY in the shell you launch claude from, or set it under the env key in .claude/settings.json so every session picks it up without touching your shell profile.

Claude Code asks to approve the command the first time it runs. Approve it, and the lookup becomes part of normal work.

From there, a question like “which region does this address belong to” turns into a script run and an answer with the JSON attached. It does the same when it writes code that needs a real location instead of an invented one.

What to watch for

A bad key fails loudly, which is what you want from a script an agent runs unattended:

ipstack error 101: You have not supplied a valid API Access Key. [Technical Support: support@apilayer.com]

Three things worth knowing before you ship this.

Watch your request count while an agent is driving. Something running in a loop gets through an allowance faster than you would by hand, so check usage in the dashboard after the first session and cache anything you resolve more than once.

Requesting a module your plan does not include returns an error rather than a partial response. Stay on the location fields until you need more. 

Keep the key in the environment. An agent that can read your source can read a hardcoded key, and anything it writes to a file can end up in a commit.

Start here

Create one IPstack account, grab your API key, and make your first geolocation call in minutes. No credit card required. Start here: 
https://dashboard.ipstack.com/signup/free/monthly

Try ipstack free

IP-to-location, ASN, ISP, time zone and threat data from one endpoint. Get a key and make your first call in under a minute.

Get Free API Key

Frequently asked questions

How do I connect an API to Claude tool use?
Define the API as a tool with a JSON input_schema, pass it in the tools list, and watch for a tool_use block in the response. Run the request, return the output as a tool_result, and Claude produces the final answer.
Can one API key power multiple AI agent tools?
Yes. One APILayer key authenticates every suite API. Above, one key drives geolocation, stock, flight, search, and news tools with no per-service setup.
Which APILayer APIs work as live-data tools for AI agents?
Any suite API that returns structured JSON. This post uses IPstack, Marketstack, Aviationstack, SERPstack, and Mediastack for location, market, flight, search, and news data.
Do I need a separate account for each APILayer API?
No. One APILayer account gives one key, one dashboard, and one invoice across the suite.
What live data can a Claude agent fetch with the APILayer suite?
Geolocation from IPstack, stock prices from Marketstack, flight status from Aviationstack, Google search results from SERPstack, and news headlines from Mediastack, all through the same key.

API

Giving models real location data instead of a training cutoff — the ipstack MCP server, agent tool use, and how the current LLMs handle the API.

Browse 1 article

Start building with ipstack

IP-to-location, ASN, ISP, time zone and threat data from a single endpoint. Get a key and make your first call in under a minute.