Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ihfaz297/MND/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The MND system uses several Google Cloud APIs:- Distance Matrix API - Calculate travel times between locations (backend)
- Directions API - Route instructions (optional)
- Maps JavaScript API - Web map display (frontend, optional)
- Maps SDK for Android - Mobile map display (Flutter Android)
- Maps SDK for iOS - Mobile map display (Flutter iOS)
Google Cloud Console Setup
1. Create Google Cloud Project
- Go to Google Cloud Console
- Sign in with your Google account
- Click Select a project → New Project
- Enter project details:
- Project name: MND Bus Routing
- Organization: (optional)
- Click Create
2. Enable Billing
- Go to Billing
- Click Link a billing account
- Create a new billing account or select existing
- Enter payment information
- $200/month free credit
- Distance Matrix: ~700 requests/month free
- After credit: $5-10 per 1,000 requests
3. Enable APIs
Go to APIs & Services → Library Enable these APIs:Distance Matrix API (Required)
- Search for “Distance Matrix API”
- Click Distance Matrix API
- Click Enable
Directions API (Optional)
- Search for “Directions API”
- Click Directions API
- Click Enable
Maps JavaScript API (Optional - Web)
- Search for “Maps JavaScript API”
- Click Maps JavaScript API
- Click Enable
Maps SDK for Android (Optional - Mobile)
- Search for “Maps SDK for Android”
- Click Maps SDK for Android
- Click Enable
Maps SDK for iOS (Optional - Mobile)
- Search for “Maps SDK for iOS”
- Click Maps SDK for iOS
- Click Enable
Creating API Keys
1. Create Credentials
- Go to APIs & Services → Credentials
- Click Create Credentials → API key
- New API key is created and displayed
2. Backend API Key (Distance Matrix)
Restrict to Distance Matrix API:- Click Restrict Key next to your newly created key
- Name: “Backend - Distance Matrix”
- Under API restrictions:
- Select Restrict key
- Check ✓ Distance Matrix API
- Check ✓ Directions API (if using)
- Under Application restrictions:
- Select IP addresses
- Add your server IP:
203.0.113.42 - For development, add:
0.0.0.0/0(remove in production!)
- Click Save
.env:
3. Web Frontend API Key (Maps JavaScript)
Restrict to Maps JavaScript API:- Create another API key
- Name: “Web Frontend - Maps”
- Under API restrictions:
- Select Restrict key
- Check ✓ Maps JavaScript API
- Under Application restrictions:
- Select HTTP referrers (web sites)
- Add your domain:
https://yourdomain.com/* - For development, add:
http://localhost:5173/*
- Click Save
.env:
4. Mobile API Key (Android/iOS)
Restrict to Mobile Maps SDKs:- Create another API key
- Name: “Mobile - Maps”
- Under API restrictions:
- Select Restrict key
- Check ✓ Maps SDK for Android
- Check ✓ Maps SDK for iOS
- Under Application restrictions:
- Select Android apps
- Add your package name:
com.yourcompany.mnd_flutter - Add SHA-1 fingerprint (see below)
- Click Save
.env:
API Key Security Best Practices
Never Commit API Keys
Add to.gitignore:
Use Different Keys for Each Environment
- Development: Unrestricted or localhost-only
- Staging: Restricted to staging domain
- Production: Restricted to production domain/IP
Rotate Keys Regularly
Rotate API keys every 90 days:- Create new API key
- Update application configuration
- Test thoroughly
- Delete old key
Monitor API Usage
Set up billing alerts:- Go to Billing → Budgets & alerts
- Click Create Budget
- Set budget amount: $50/month
- Set alert thresholds: 50%, 75%, 90%, 100%
- Add email for notifications
Quota Management
Free Tier Limits
Distance Matrix API:- $200 credit/month ≈ 700-1,000 requests
- After credit: $5-10 per 1,000 requests
- Dynamic Maps: $7 per 1,000 loads
- Static Maps: $2 per 1,000 loads
- First 28,500 loads free
Quota Monitoring
Check usage in Google Cloud Console → APIs & Services → Dashboard Real-time monitoring:Set Quota Limits
Prevent unexpected charges:- Go to APIs & Services → Distance Matrix API → Quotas
- Click Queries per day
- Edit quota:
- Set to 50 requests/day (development)
- Set to 100 requests/day (production)
- Click Save
The backend has built-in limits (700/month, 50/day) but Google Cloud quotas provide an additional safety net.
Caching Strategy
The MND backend caches Distance Matrix results to minimize API calls.Cache Configuration
Insrc/infra/distanceMatrixClient.ts:
Cache Benefits
- Reduces API calls: 80-90% cache hit rate achievable
- Faster responses: No network latency
- Cost savings: Free cached results
Cache Key Format
Pre-warming Cache
Populate cache with common routes:- Reads all node pairs from
nodes.json - Calls Distance Matrix API for each pair
- Caches results in memory and
edges.json - Respects quota limits
Cache Invalidation
Cache expires after 7 days. To force refresh:Distance Matrix API Details
Request Format
Parameters
Starting location (address or coordinates).Examples:
"Tilagor, Sylhet, Bangladesh""24.8949,91.8687"
Ending location (address or coordinates).
Travel mode.Valid values:
driving- Car/bus routeswalking- Pedestrian routesbicycling- Bike routestransit- Public transit (requires transit API)
Your API key.
Response Format
Error Handling
Common status codes:OK- SuccessZERO_RESULTS- No route foundOVER_QUERY_LIMIT- Quota exceededREQUEST_DENIED- Invalid API key or restrictionsINVALID_REQUEST- Missing parametersUNKNOWN_ERROR- Server error, retry
Testing API Setup
Test Distance Matrix API
Create a test script:Test Maps JavaScript API
Createtest.html:
API Cost Estimation
Distance Matrix API
Free tier: $200/month = ~700-1,000 requests Pricing tiers:- 0-100,000 requests: $5 per 1,000 requests
- 100,001-500,000: $4 per 1,000 requests
- 500,001+: Contact sales
Maps JavaScript API
Free tier: 28,500 map loads/month Pricing:- Dynamic Maps: $7 per 1,000 loads
- Static Maps: $2 per 1,000 loads
Troubleshooting
”REQUEST_DENIED” Error
Causes:- API not enabled in Cloud Console
- API key invalid
- API key restrictions blocking request
- Verify API is enabled: Enable APIs
- Check API key is correct
- Review key restrictions (IP, referrer, bundle ID)
“OVER_QUERY_LIMIT” Error
Causes:- Exceeded quota limit
- Billing not enabled
- Check quota usage
- Verify billing account is linked
- Increase quota limits
- Implement caching (already done in MND)
Quota Exceeded But Usage Looks Low
Possible causes:- Multiple projects using same billing account
- Quota reset happens monthly, not daily
- Cache not working (check cache hit rate)
API Key Not Working on Mobile
Android:- Verify package name matches:
com.yourcompany.mnd_flutter - Check SHA-1 fingerprint is correct
- Use release keystore SHA-1 for production
- Verify bundle identifier matches
- Check API key is added to AppDelegate.swift
- Ensure Maps SDK for iOS is enabled
Production Checklist
- Google Cloud project created
- Billing account linked
- Distance Matrix API enabled
- Backend API key created and restricted
- Web API key created (if using maps)
- Mobile API key created (if using Flutter)
- API keys restricted by IP/domain/bundle ID
- API keys stored in .env (not committed to git)
- Quota limits configured
- Billing alerts set up ($50 threshold)
- Cache strategy implemented
- API tested successfully
- Monitoring configured