Setup Google Maps API for Distance Shipping
Looking for Magento 2 distance-based shipping extension? View the full Magento 2 distance-based shipping extension page to explore features, configuration options, and pricing.
Configure your Google Maps API key for the Shipping Costs Based on Distance extension. This guide walks you through creating a Google Cloud project, enabling the required APIs, and setting up your credentials.
Required API
This extension supports two Google Maps APIs:
- Routes API (Recommended) - The new API with improved performance and features
- Distance Matrix API (Legacy) - The older API for backward compatibility
You can select which API to use in the configuration panel. Both APIs calculate travel distances between your store location and customer addresses.
For New Installations: We recommend using the Routes API.
For Existing Installations: Your installation will continue using Distance Matrix API by default. You can migrate to Routes API at your convenience by enabling it in Google Cloud Console and switching the configuration.
Step 1: Create a Google Cloud Project
- Go to Google Cloud Console
- Sign in with your Google account
- Click the project dropdown at the top of the page
- Click New Project
- Enter a project name (e.g., "Magento Shipping")
- Click Create
- Wait for the project to be created, then select it
Step 2: Enable Billing
Both Google Maps APIs require a billing account. Google provides $200 free credit per month.
- Go to Billing in the left menu
- Click Link a billing account
- Follow the prompts to set up billing
- Add a payment method
Note: You won't be charged unless you exceed the free tier limits.
Step 3: Enable the API
Choose which API to enable based on your preference:
Option A: Routes API (Recommended for new installations)
- Go to APIs & Services → Library
- Search for "Routes API"
- Click on Routes API
- Click Enable
Option B: Distance Matrix API (Legacy - for existing installations)
- Go to APIs & Services → Library
- Search for "Distance Matrix API"
- Click on Distance Matrix API
- Click Enable
Note: You can enable both APIs if you want to test migration between them.
Step 4: Create API Key
- Go to APIs & Services → Credentials
- Click + Create Credentials
- Select API key
- Your new API key will be displayed
- Click Copy to copy the key
- Click Close
Step 5: Restrict Your API Key (Recommended)
Protect your API key from unauthorized use:
- In the Credentials page, click on your API key name
- Under Application restrictions:
- For testing: Select None
- For production: Select IP addresses and add your server IP
- Under API restrictions:
- Select Restrict key
- Check the API you're using:
- Routes API (if using the new API)
- Distance Matrix API (if using the legacy API)
- You can check both if you want to test migration
- Click Save
Step 6: Configure in Magento
- Go to Magmodules → Shipping Costs Based on Distance
- Under General → API and Location Details:
- Google Maps API Version: Select which API to use
- Routes API (Recommended) - Use the new Routes API
- Distance Matrix API (Legacy) - Use the legacy API (default for existing installations)
- Paste your API key in the API Key field
- Enter your Store Address (full address including country)
- Google Maps API Version: Select which API to use
- Click Test Credentials to verify the connection
- Click Save Config
Important: Make sure the API version you select matches the API you enabled in Google Cloud Console.
API Pricing
Google charges per request. Pricing varies by API:
- Routes API (Compute Route Matrix): See current pricing at Google Maps Platform Pricing
- Distance Matrix API (Legacy): See current pricing at Google Maps Platform Pricing
Monthly free credit: $200
Pricing may vary by region and usage tier. The Routes API generally offers better performance and may have different pricing tiers than the legacy Distance Matrix API.
When are requests made?
- When a customer enters/changes their shipping address at checkout
- When using the product page shipping estimator
- During selftest/credential testing
Monitoring Usage
Track your API usage to manage costs:
- Go to APIs & Services → Dashboard
- Click on the API you're using:
- Routes API (if using the new API)
- Distance Matrix API (if using the legacy API)
- View the Traffic chart for request counts
- Check Errors for any failed requests
Set Up Billing Alerts
- Go to Billing → Budgets & alerts
- Click Create Budget
- Set a budget amount
- Configure alert thresholds (e.g., 50%, 90%, 100%)
- Add notification email addresses
Store Address Best Practices
The store address you enter is the origin point for all distance calculations.
Do:
- Use your warehouse or fulfillment center address
- Include full address: street, city, postal code, country
- Use the address format Google Maps recognizes
Examples of Good Addresses:
123 Warehouse Street, Amsterdam, 1012 AB, Netherlands
456 Distribution Center Dr, Rotterdam, 3011 BC, NL
Don't:
- Use PO Box addresses
- Use incomplete addresses
- Use addresses Google can't find
Testing Your Setup
After configuration, verify everything works:
-
Test Credentials Button
- Click the button in configuration
- Should show success message
-
Selftest Command
- Run:
bin/magento distancebasedshipping:selftest - Checks API connectivity and configuration
- Run:
-
Checkout Test
- Add a product to cart
- Go to checkout
- Enter a shipping address
- Verify distance-based shipping option appears
Troubleshooting API Issues
"API key not valid"
- Verify the key is copied correctly (no extra spaces)
- Check the key is enabled in Google Cloud Console
- Ensure the correct API is enabled for the project (Routes API or Distance Matrix API)
- Verify the API version setting in Magento matches the API enabled in Google Cloud Console
"This API project is not authorized"
- Enable the appropriate API in your project (Routes API or Distance Matrix API)
- Ensure the API version in Magento configuration matches what's enabled in Google Cloud Console
- Wait a few minutes after enabling for changes to propagate
"You have exceeded your daily request quota"
- Check your usage in Google Cloud Console
- Upgrade your billing plan if needed
- The free tier resets monthly
"REQUEST_DENIED"
- Check API key restrictions aren't blocking your server
- Verify billing is set up correctly
- Ensure the API is enabled
"ZERO_RESULTS"
- Store address may not be recognized by Google
- Try a more specific address format
- Test the address in Google Maps first
Need More Help?
Documentation:
- All Help Articles - Complete documentation overview
Support:
- Contact Support - Get help from our team
Google Resources:
For a complete overview of features and configuration options, see Magento 2 distance-based shipping extension.