Pular para o conteúdo

JavaScript

Use fetch, nativo no Node.js 18+ e nos navegadores. Não há SDK: um cliente de poucas linhas cobre autenticação, erros e paginação.

Requisitos

Node.js 18 ou mais recente (fetch nativo). Em TypeScript o mesmo código funciona sem mudanças.

Cliente mínimo

Envia o token, a clínica ativa quando houver e transforma o erro padrão em exceção com o requestId.

const BASE_URL = "https://myclinc.leadpoint.com.br/v1"​export async function api(path, { token, clinicId, method = "GET", body } = {}) {  const res = await fetch(BASE_URL + path, {    method,    headers: {      Authorization: `Bearer ${token}`,      ...(clinicId ? { "X-Clinic-Id": clinicId } : {}),      ...(body ? { "Content-Type": "application/json" } : {}),    },    body: body ? JSON.stringify(body) : undefined,  })  if (res.status === 204) return null  const json = await res.json()  if (!res.ok) {    const { code, message, requestId } = json.error    throw new Error(`${res.status} ${code}: ${message} (requestId ${requestId})`)  }  return json}

Percorrer uma lista

Listas usam cursor: repita enquanto meta.hasNext for verdadeiro. Detalhes em Paginação e filtros.

let cursor = nulldo {  const qs = new URLSearchParams({ limit: "100", ...(cursor ? { cursor } : {}) })  const { data, meta } = await api(`/patients?${qs}`, { token })  for (const paciente of data) console.log(paciente.id, paciente.name)  cursor = meta.hasNext ? meta.nextCursor : null} while (cursor)

Exemplo da referência

Gerado do contrato para listar pacientes (GET /v1/patients).

const res = await fetch("https://myclinc.leadpoint.com.br/v1/patients?limit=25", {  method: "GET",  headers: {    Authorization: "Bearer <TOKEN>",    "X-Clinic-Id": "<clinic_id>",  },})​if (!res.ok) {  const { error } = await res.json()  throw new Error(`${error.code}: ${error.message} (${error.requestId})`)}const { data } = await res.json()
Troque os marcadores entre < > pelos seus valores. O token nunca deve ficar no código-fonte: leia de uma variável de ambiente.