Google Maps API Setup Guide for Magento 2
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
The extension requires the Distance Matrix API from Google Cloud Platform. This API calculates travel distances between your store location and customer addresses.
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
The Distance Matrix API requires 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 Distance Matrix API
- Go to APIs & Services → Library
- Search for "Distance Matrix API"
- Click on Distance Matrix API
- Click Enable
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 Distance Matrix API
- Click Save
Step 6: Configure in Magento
- Go to Magmodules → Shipping Costs Based on Distance
- Under General → API and Location Details:
- Paste your API key in the API Key field
- Enter your Store Address (full address including country)
- Click Test Credentials to verify the connection
- Click Save Config
API Pricing
Google charges per request to the Distance Matrix API:
| Tier | Price per 1,000 requests |
|---|---|
| 0 - 100,000 | $5.00 |
| 100,001 - 500,000 | $4.00 |
| 500,001+ | Contact Google |
Monthly free credit: $200 (covers ~40,000 requests)
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 Distance Matrix 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 Distance Matrix API is enabled for the project
"This API project is not authorized"
- Enable Distance Matrix API in your project
- 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: