This buildpack downloads and installs MaxMind GeoIP2 databases for use in your Heroku application.
- Add the buildpack to your Heroku app:
heroku buildpacks:add https://github.com/your-repo/heroku-maxmind-geoip-buildpack
- Set your MaxMind license key:
heroku config:set MAXMIND_LICENSE_KEY=your_license_key_here
MAXMIND_LICENSE_KEY
: Your MaxMind license key (required)
-
MAXMIND_EDITIONS
: Space-separated list of database editions to install- Default:
GeoLite2-Country
- Available options:
GeoLite2-Country
,GeoLite2-City
,GeoLite2-ASN
- Example:
heroku config:set MAXMIND_EDITIONS="GeoLite2-Country GeoLite2-City"
- Default:
-
MAXMIND_DB_DIR
: Base directory for database files- Default:
$HOME/vendor
- Usually shouldn't need to change this
- Default:
The buildpack automatically sets these variables for your application:
MAXMIND_GEOLITE2_COUNTRY_PATH
: Full path to the Country databaseMAXMIND_GEOLITE2_CITY_PATH
: Full path to the City database (if installed)MAXMIND_GEOLITE2_ASN_PATH
: Full path to the ASN database (if installed)
require 'maxmind-db'
# Basic usage (Country database)
reader = MaxMind::DB.new(ENV['MAXMIND_GEOLITE2_COUNTRY_PATH'])
result = reader.get('8.8.8.8')
# Using multiple databases
if ENV['MAXMIND_GEOLITE2_CITY_PATH']
city_reader = MaxMind::DB.new(ENV['MAXMIND_GEOLITE2_CITY_PATH'])
city_result = city_reader.get('8.8.8.8')
end
import geoip2.database
import os
# Basic usage
with geoip2.database.Reader(os.environ['MAXMIND_GEOLITE2_COUNTRY_PATH']) as reader:
response = reader.country('8.8.8.8')
country_code = response.country.iso_code
const Reader = require('@maxmind/db-reader');
const fs = require('fs');
// Basic usage
const buffer = fs.readFileSync(process.env.MAXMIND_GEOLITE2_COUNTRY_PATH);
const reader = new Reader(buffer);
const result = reader.get('8.8.8.8');
Database | Size (approx) | Use Case |
---|---|---|
GeoLite2-Country | 7MB | Basic country detection, geo-blocking |
GeoLite2-City | 70MB | Detailed location info, localization |
GeoLite2-ASN | 7MB | Network/ISP detection, VPN identification |
- Databases are cached to speed up deployments
- Cache is invalidated weekly to ensure fresh data
- Force a fresh download by clearing the build cache:
heroku builds:cache:purge -a your-app-name
To run the compile script locally using our test directory structure, follow these steps:
- Ensure test directories exist:
mkdir -p test/build/vendor test/cache/maxmind test/env
- Set up test credentials (if you haven't already):
echo "your_license_key" > test/env/MAXMIND_LICENSE_KEY
echo "your_account_id" > test/env/MAXMIND_ACCOUNT_ID
- Run the compile script with the test paths:
./bin/compile "$PWD/test/build" "$PWD/test/cache" "$PWD/test/env"