import requests def get_place_id(api_key, query): # Google Places API endpoint endpoint = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json" # Parameters params = { "input": query, # Input can be the name or address "inputtype": "textquery", "fields": "place_id", # Request only the `place_id` field "key": api_key } # API Call response = requests.get(endpoint, params=params) # Check if the request was successful if response.status_code == 200: data = response.json() if "candidates" in data and len(data["candidates"]) > 0: return data["candidates"][0]["place_id"] # Return the first candidate's place_id else: return f"No results found for query: {query}" else: return f"Error: {response.status_code} - {response.text}" # Example usage if __name__ == "__main__": # Replace with your API key YOUR_API_KEY = "your_google_api_key" QUERY = "Le Bernardin, New York, NY" place_id = get_place_id(YOUR_API_KEY, QUERY) print(f"Place ID: {place_id}")