Client HTTP minimal
Helper Node.js (ESM) pour rejouer les recettes de ce cookbook. Il accepte Bearer ou cookie de session (voir Authentification).
- Node.js
- curl
- GraphQL
// ubloClient.mjs
const ENDPOINT =
process.env.UBLO_GRAPHQL_URL ?? 'https://example.ublo.immo/api/graphql';
/**
* @param {string} query
* @param {Record<string, unknown>} [variables]
* @param {{ token?: string, cookieJar?: string }} [auth]
*/
export async function ublo(query, variables, auth = {}) {
const token = auth.token ?? process.env.UBLO_API_TOKEN;
const cookieJar = auth.cookieJar ?? process.env.UBLO_COOKIE;
if (!token && !cookieJar) {
throw new Error('Provide UBLO_API_TOKEN (Bearer) or UBLO_COOKIE (session)');
}
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
};
if (token) headers.Authorization = `Bearer ${token}`;
if (cookieJar) headers.Cookie = cookieJar;
const res = await fetch(ENDPOINT, {
method: 'POST',
headers,
body: JSON.stringify({ query, variables }),
});
const json = await res.json();
// GraphQL peut renvoyer HTTP 200 + errors[]
if (json.errors?.length) {
const detail = JSON.stringify(json.errors, null, 2);
throw new Error(`GraphQL errors:\n${detail}`);
}
return json.data;
}
Usage (Bearer via env) :
import { ublo } from './ubloClient.mjs';
const data = await ublo(`
query GetCompany {
company { id name }
}
`);
Usage (cookie de session) :
const data = await ublo(
`query GetCompany { company { id name } }`,
undefined,
{ cookieJar: process.env.UBLO_COOKIE }, // ex. "UBLO_TOKEN_AUTHENTICATION=…"
);
- Bearer
- Cookie session
export UBLO_GRAPHQL_URL='https://example.ublo.immo/api/graphql'
export UBLO_API_TOKEN='eyJ…'
curl -sS -X POST "$UBLO_GRAPHQL_URL" \
-H "Authorization: Bearer $UBLO_API_TOKEN" \
-H 'Content-Type: application/json' \
-d '{"query":"query { company { id name } }"}' | jq .
export UBLO_GRAPHQL_URL='https://example.ublo.immo/api/graphql'
# cookies.txt produit par : curl … -c cookies.txt … (mutation login)
curl -sS -X POST "$UBLO_GRAPHQL_URL" \
-H 'Content-Type: application/json' \
-b cookies.txt \
-d '{"query":"query { company { id name } }"}' | jq .
query GetCompany {
company {
id
name
}
}
Variantes d’auth dans ce cookbook
Chaque onglet curl propose Bearer et Cookie session (groupId="auth"). Le helper Node lit UBLO_API_TOKEN ou UBLO_COOKIE.
Checklist d’intégration
| Point | Pourquoi |
|---|---|
Un seul endpoint POST /api/graphql | Pas de REST métier public pour les intégrations |
Lire errors[] même en HTTP 200 | Pattern GraphQL standard Ublo |
| Demander uniquement les champs utiles | Perf + contrats plus stables |
Stocker externalId / customReference | Réconciliation avec votre SI |
Ne pas utiliser /search | Route interne non supportée pour les intégrations |
Voir aussi les bonnes pratiques officielles.