JavaScript & Node.js Integration Guide
Integrate BayutAPI into your JavaScript or Node.js application using fetch and async/await. Covers authentication, property search, pagination, and error handling.
Prerequisites
- Node.js 18+ (for native fetch support) or any modern browser
- A RapidAPI account with a BayutAPI subscription (get your free API key)
- Basic knowledge of JavaScript and async/await
Installation
If you are using Node.js 18+, the native fetch API is available globally. For older versions, install node-fetch:
npm install node-fetch
Alternatively, you can use axios:
npm install axios
Authentication
Set up your API headers. Every request to BayutAPI needs these two headers:
const API_KEY = "YOUR_API_KEY";
const BASE_URL = "https://bayut14.p.rapidapi.com";
const headers = {
"x-rapidapi-key": API_KEY,
"x-rapidapi-host": "bayut14.p.rapidapi.com",
};
Replace YOUR_API_KEY with the key from your RapidAPI dashboard.
Basic Example: Search Properties
Search for rental properties using the fetch API:
async function searchProperties(purpose, locationId, page = 1) {
const params = new URLSearchParams({
purpose,
location_ids: locationId,
page: page.toString(),
});
const response = await fetch(`${BASE_URL}/search-property?${params}`, {
method: "GET",
headers,
});
if (!response.ok) {
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
return response.json();
}
// Search for rentals in Dubai Marina
const results = await searchProperties("for-rent", "5002");
console.log(`Total results: ${results.data.total}`);
console.log(`Page ${results.data.page} of ${results.data.totalPages}`);
results.data.properties.forEach((property) => {
console.log(`${property.title.en} - AED ${property.price.toLocaleString()}`);
});
Location Autocomplete
Find location IDs using the autocomplete endpoint:
async function autocomplete(query) {
const params = new URLSearchParams({ query, langs: "en" });
const response = await fetch(`${BASE_URL}/autocomplete?${params}`, {
method: "GET",
headers,
});
if (!response.ok) {
throw new Error(`Autocomplete failed: ${response.statusText}`);
}
const data = await response.json();
return data.data.locations.map((hit) => ({
name: hit.name,
id: hit.externalID,
type: hit.type,
}));
}
// Find locations matching "downtown"
const locations = await autocomplete("downtown dubai");
locations.forEach((loc) => {
console.log(`${loc.name} (ID: ${loc.id}, Type: ${loc.type})`);
});
Advanced Example: Fetch All Pages
Paginate through all results to build a complete dataset:
async function fetchAllListings(locationId, purpose = "for-sale", maxPages = 5) {
const allListings = [];
let page = 1;
while (page <= maxPages) {
const params = new URLSearchParams({
purpose,
location_ids: locationId,
page: page.toString(),
});
const response = await fetch(`${BASE_URL}/search-property?${params}`, {
method: "GET",
headers,
});
if (!response.ok) break;
const data = await response.json();
const hits = data.data.properties;
if (hits.length === 0) break;
allListings.push(...hits);
console.log(`Page ${page}/${data.data.totalPages}: ${hits.length} listings`);
if (page >= data.data.totalPages) break;
page++;
}
return allListings;
}
const listings = await fetchAllListings("5002", "for-sale", 3);
console.log(`\nTotal fetched: ${listings.length} listings`);
Using Axios
If you prefer axios, the same calls look like this:
import axios from "axios";
const api = axios.create({
baseURL: "https://bayut14.p.rapidapi.com",
headers: {
"x-rapidapi-key": "YOUR_API_KEY",
"x-rapidapi-host": "bayut14.p.rapidapi.com",
},
});
// Search properties
const { data } = await api.get("/search-property", {
params: {
purpose: "for-sale",
location_ids: "5002",
page: "1",
},
});
console.log(`Found ${data.data.total} properties`);
Error Handling
Wrap API calls with proper error handling for production use:
async function safeApiCall(endpoint, params = {}) {
try {
const queryString = new URLSearchParams(params).toString();
const url = `${BASE_URL}${endpoint}?${queryString}`;
const response = await fetch(url, { method: "GET", headers });
if (response.status === 429) {
console.error("Rate limit exceeded. Wait before retrying.");
return null;
}
if (response.status === 400) {
const error = await response.json();
console.error(`Bad request: ${error.message}`);
return null;
}
if (!response.ok) {
console.error(`HTTP error: ${response.status}`);
return null;
}
return await response.json();
} catch (error) {
console.error(`Request failed: ${error.message}`);
return null;
}
}
// Usage
const result = await safeApiCall("/search-property", {
purpose: "for-sale",
page: "1",
});
if (result) {
console.log(`Found ${result.data.total} properties`);
}
Next Steps
- Check out the React Integration Guide for building property search UIs
- Explore the Next.js Guide for server-rendered property pages
- Browse the full API documentation for all endpoints and parameters
- Test endpoints on the RapidAPI playground