Using the OpenCage Geocoding API from the command line
The API is is accessed via an HTTP GET request with a few parameters. There is no need to set any headers or cookies. Note that you will need to URI encode the query for forward geocoding (address to coordinates) requests.curl
# reverse geocoding
curl 'https://api.opencagedata.com/geocode/v1/json?q=-23.5373732,-46.8374628&pretty=1&key=YOUR-API-KEY'
# forward
curl 'https://api.opencagedata.com/geocode/v1/json?q=Philipsbornstra%C3%9Fe+2,+30165+Hanover,+Germany&pretty=1&key=YOUR-API-KEY'
HTTPie
http GET https://api.opencagedata.com/geocode/v1/json q==-23.5373732,-46.8374628 pretty==1 key==YOUR-API-KEY
wget
wget 'https://api.opencagedata.com/geocode/v1/json?q=-23.5373732,-46.8374628&pretty=1&key=YOUR-API-KEY' -O results.json
OpenCage CLI tool
We provide an installable command line tool to geocode CSV files. It supports parallel requests, selecting input and output columns, progress bar, error handling. Requires Python 3.8 or newer.
See our full geocoding CLI tutorial.
pip install opencage
opencage -h
cat coordinates.csv
49.403370,2.325642
48.048349,-1.601447
44.504731,1.188501
43.606452,-1.381768
opencage reverse --api-key YOUR-API-KEY --input coordinates.csv --output results.csv --add-columns country,formatted
cat results.csv
49.403370,2.325642,France,"1 Rue du Général Leclerc, 60510 La Neuville-en-Hez, France"
48.048349,-1.601447,France,"9 Allée du Close Suzan, 35770 Vern-sur-Seiche, France"
44.504731,1.188501,France,"1 Rue du Quercy, 46220 Prayssac, France"
43.606452,-1.381768,France,"1635 Route du Tuc, 40230 Orx, France"