Geo API
Location storage and distance/area searches using geospatial queries
Geo endpoints store named locations and perform distance and area searches. Under the hood, Redis stores geospatial members in sorted sets.
Endpoints
| Method | Endpoint | Description |
|---|---|---|
| POST | /{plateId}/geo/add | Add locations |
| POST | /{plateId}/geo/positions | Get coordinates |
| GET | /{plateId}/geo/distance/{key} | Get distance between members |
| POST | /{plateId}/geo/search | GEOSEARCH |
| POST | /{plateId}/geo/search/store | GEOSEARCHSTORE |
| POST | /{plateId}/geo/command | Execute allowed geo commands |
| POST | /{plateId}/geo/{key}/command | Execute key-specific geo commands |
Examples
Add locations
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/add`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
key: "cities",
locations: [
{ member: "San Francisco", longitude: -122.4194, latitude: 37.7749 },
{ member: "Los Angeles", longitude: -118.2437, latitude: 34.0522 }
]
})
});
const data = await response.json();
console.log(data);Get positions
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/positions`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
key: "cities",
members: ["San Francisco", "Los Angeles"]
})
});
const data = await response.json();
console.log(data);Get distance
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/distance/cities?from=San%20Francisco&to=Los%20Angeles&unit=km`, {
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
}
});
const data = await response.json();
console.log(data);Search by radius
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/search`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
key: "cities",
from_member: "San Francisco",
radius: 600,
unit: "km",
with_dist: true,
sort: "asc"
})
});
const data = await response.json();
console.log(data);Search by box and store the result
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/search/store`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
key: "cities",
destination: "cities:west-coast",
from_lon: -122.4194,
from_lat: 37.7749,
width: 800,
height: 800,
unit: "km"
})
});
const data = await response.json();
console.log(data);Command Compatibility
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/command`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
command: "GEOADD",
args: ["cities", -122.4194, 37.7749, "San Francisco"]
})
});
const data = await response.json();
console.log(data);Command Endpoints
POST /{plateId}/geo/command
Execute allowed geo commands across the plate.
Allowed Commands:
| Command | Description |
|---|---|
| GEOADD | Add locations |
| GEOPOS | Get coordinates |
| GEODIST | Get distance |
| GEOSEARCH | Search locations |
| GEOSEARCHSTORE | Search and store results |
Query Parameters for GEODIST:
from(required): First memberto(required): Second memberunit(optional): Distance unit -m(meters),km(kilometers),mi(miles),ft(feet). Default:m
POST /{plateId}/geo/{key}/command
Execute allowed commands on a specific geo key.
Allowed Commands: Same as above.
Request:
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/geo/cities/command`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
command: "GEOPOS",
args: ["San Francisco"]
})
});
const data = await response.json();
console.log(data);