Developers
Idempotency and upserts
Networks fail. Two tools keep retries from creating duplicate homes: the Idempotency-Key header and your own reference on each home.
Idempotency-Key
Send an Idempotency-Key header with any POST. A UUID per operation is a good choice.
- The same key with the same body within 24 hours returns the stored response, without doing the work again.
- The same key with a different body returns
409with the codeidempotency_conflict. - After 24 hours a key can be used again.
Generate the key when you decide to do the operation and keep it with the job, so every retry of that job sends the same key.
curl -X POST https://api.instaenv.com/api/public/v1/properties \
-H "Authorization: Bearer $INSTAENV_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 6f1c2b8e-1d0b-4c59-9a43-2f0f5d7b9e21" \
-d '{"title": "Piso en Gràcia", "reference": "ABC123", "operation": "sale", "price": 345000}'Your reference
reference is your own ID for the home (the listing ID in your CRM). It is optional and, when set, unique within your agency. Filter by it to find a home without storing Instaenv's ID:
const API = "https://api.instaenv.com/api/public/v1";
const headers = {
Authorization: `Bearer ${process.env.INSTAENV_API_KEY}`,
"Content-Type": "application/json",
};
/** Creates the home the first time and updates it afterwards, keyed by your CRM's own ID. */
async function upsertProperty(crmListing) {
const fields = {
reference: crmListing.id,
title: crmListing.title,
city: crmListing.city,
price: crmListing.price,
};
const found = await fetch(`${API}/properties?reference=${encodeURIComponent(crmListing.id)}`, { headers });
const { data } = await found.json();
const res = data.length
? await fetch(`${API}/properties/${data[0].id}`, { method: "PATCH", headers, body: JSON.stringify(fields) })
: await fetch(`${API}/properties`, {
method: "POST",
headers: { ...headers, "Idempotency-Key": `create-${crmListing.id}` },
body: JSON.stringify(fields),
});
if (!res.ok) throw new Error(`Instaenv API: ${res.status}`);
return res.json();
}A second home with a reference already in use is refused with a validation error on reference.