GET
Extract Location ID API
Parse TripAdvisor hotel URLs to extract the location ID needed for API calls.
Overview
URL Parser Utility
Extract location IDs from any TripAdvisor hotel URL format. Essential for using location-based endpoints.
The Extract Location ID endpoint is a utility function that parses TripAdvisor URLs and extracts the location ID. This ID is required for many other TripAdvisor API endpoints.
Endpoint URL
GET https://api.stayapi.com/v1/tripadvisor/location/extract-id
Query Parameters
Parameter | Type | Required | Description |
---|---|---|---|
url | string | Required | Any valid TripAdvisor hotel URL |
Understanding Location IDs
URL Structure
TripAdvisor URLs contain location IDs in this format:
https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-Hotel_Name.html
- g1224250 - Geographic location ID (city/region)
- d305165 - Property location ID (specific hotel)
- This API extracts the property ID (the number after "d")
Supported URL Formats
Standard URLs
https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html
With Page Numbers
https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-or10-The_Siam.html
Different Domains
https://www.tripadvisor.co.uk/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html
https://www.tripadvisor.de/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html
Mobile URLs
https://m.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html
Common Use Cases
- Preprocessing URLs before calling other TripAdvisor endpoints
- Building URL-to-ID mapping databases
- Validating TripAdvisor URLs in user input
- Extracting IDs from scraped or collected URLs
- Converting between URL formats and location IDs
Batch Processing
When processing multiple URLs, extract all IDs first, then use the location-based endpoints for better performance.
Request
curl -X GET "https://api.stayapi.com/v1/tripadvisor/location/extract-id?url=https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html" \ -H "x-api-key: YOUR_API_KEY"
// Single URL extraction const hotelUrl = "https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html"; const response = await fetch( `https://api.stayapi.com/v1/tripadvisor/location/extract-id?url=${encodeURIComponent(hotelUrl)}`, { headers: { "x-api-key": "YOUR_API_KEY" } } ); const data = await response.json(); console.log(`Location ID: ${data.data.location_id}`); console.log(`Original URL: ${data.data.url}`); // Batch processing example const urls = [ "https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-Hotel1.html", "https://www.tripadvisor.com/Hotel_Review-g293916-d8622290-Reviews-Hotel2.html", "https://www.tripadvisor.com/Hotel_Review-g295424-d1234567-Reviews-Hotel3.html" ]; // Extract all IDs const locationIds = await Promise.all( urls.map(async (url) => { const res = await fetch( `https://api.stayapi.com/v1/tripadvisor/location/extract-id?url=${encodeURIComponent(url)}`, { headers: { "x-api-key": "YOUR_API_KEY" } } ); const data = await res.json(); return data.data.location_id; }) ); console.log("Extracted Location IDs:", locationIds); // Now use these IDs with other endpoints
import requests from urllib.parse import urlparse # Extract ID from a single URL hotel_url = "https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html" url = "https://api.stayapi.com/v1/tripadvisor/location/extract-id" headers = {"x-api-key": "YOUR_API_KEY"} params = {"url": hotel_url} response = requests.get(url, headers=headers, params=params) data = response.json() print(f"Location ID: {data['data']['location_id']}") print(f"Original URL: {data['data']['url']}") # Batch processing function def extract_location_ids(urls, api_key): """Extract location IDs from multiple TripAdvisor URLs""" location_ids = {} for hotel_url in urls: try: response = requests.get( "https://api.stayapi.com/v1/tripadvisor/location/extract-id", headers={"x-api-key": api_key}, params={"url": hotel_url} ) data = response.json() if data["success"]: location_ids[hotel_url] = data["data"]["location_id"] print(f"✓ Extracted ID {data['data']['location_id']} from {urlparse(hotel_url).netloc}") else: print(f"✗ Failed to extract ID from {hotel_url}") except Exception as e: print(f"✗ Error processing {hotel_url}: {str(e)}") return location_ids # Example usage urls_to_process = [ "https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html", "https://www.tripadvisor.co.uk/Hotel_Review-g293916-d8622290-Reviews-Mandarin_Oriental.html", "https://m.tripadvisor.com/Hotel_Review-g295424-d1234567-Reviews-Hotel_Example.html" ] extracted_ids = extract_location_ids(urls_to_process, "YOUR_API_KEY") # Use the extracted IDs print(f"\nExtracted {len(extracted_ids)} location IDs") for url, loc_id in extracted_ids.items(): print(f" {loc_id} <- {url[:50]}...")
Response
{ "success": true, "data": { "location_id": "305165", "url": "https://www.tripadvisor.com/Hotel_Review-g1224250-d305165-Reviews-The_Siam.html" }, "message": "Successfully extracted location ID", "retrieved_at": "2024-01-15T10:30:00Z" }
{ "error": "Invalid URL format", "detail": "Could not extract location ID from URL. Please provide a valid TripAdvisor hotel URL.", "status_code": 400 }