Endpoint
Query parameters
The latitude of the search location in decimal degrees. Must be a valid floating-point number between -90 and 90.Example:
37.7749The longitude of the search location in decimal degrees. Must be a valid floating-point number between -180 and 180.Example:
-122.4194The search radius in meters. Must be between 100 and 100,000 meters (100km).
- Minimum:
100 - Maximum:
100000 - Default:
8000
Optional search query to filter coffee shops by name. The search is case-insensitive and uses partial matching.Example:
starbucksResponse
Array of coffee shop objects, sorted by distance (closest first). Limited to a maximum of 60 results.
Request examples
Response examples
Error codes
400 Bad Request
Returned when the request contains invalid or missing parameters:| Error Message | Cause | Solution |
|---|---|---|
Latitude and longitude are required | Missing lat or lng parameter | Include both lat and lng query parameters |
Invalid latitude or longitude | Non-numeric or unparseable coordinate values | Ensure lat and lng are valid decimal numbers |
Radius must be between 100 and 100000 meters | Radius outside valid range | Use a radius between 100 and 100,000 meters |
Implementation details
Data source integration
The endpoint queries OpenStreetMap data via the Overpass API:- Searches for nodes, ways, and relations tagged with
amenity=cafe - Applies a radial search using the
aroundfilter - Optionally filters by name using case-insensitive regex matching
- Returns center coordinates and all available tags
Query sanitization
Thequery parameter is sanitized before being used in the Overpass query:
- Leading and trailing whitespace is removed
- Double quotes (
") are stripped - Backslashes (
\) are removed - Empty queries are treated as no filter
Address formatting
Addresses are constructed from OpenStreetMap tags in the following priority:addr:housenumber+addr:street,addr:cityaddr:streetandaddr:cityonlyaddr:city,addr:town, oraddr:villageaddr:fulltag- “Address not available” fallback
Response processing
- Results are sorted by distance (closest first)
- Maximum of 60 results returned
- Elements without valid coordinates are filtered out
- Distance is calculated using the Haversine formula
Caching
The endpoint uses Next.js revalidation with a 120-second cache period to reduce load on the Overpass API.Fallback behavior
If the Overpass API request fails (network error, timeout, or non-200 response), the endpoint returns mock data instead of an error. This ensures the application remains functional during external service outages.
TypeScript interfaces
Best practices
Use appropriate radius
Start with smaller radius values (1000-5000m) for urban areas. Larger values may return too many results or timeout.
Handle empty results
Always check if the
shops array is empty. Remote areas may have no coffee shops in the OpenStreetMap database.Cache responses
Coffee shop data doesn’t change frequently. Consider caching responses client-side for 2-5 minutes.
Validate coordinates
Validate latitude and longitude on the client before making requests to avoid 400 errors.
Rate limiting considerations
While the Coffee Finder API doesn’t enforce rate limits, the underlying Overpass API has usage policies:- Requests timeout after 25 seconds
- Avoid making more than 1 request per second
- The endpoint caches responses for 120 seconds
- Consider debouncing user input when implementing search
Next steps
API overview
Learn about authentication and general API concepts
Quick start
Get started with Coffee Finder in minutes