Developer docs

Errors

When a call fails, the API answers with an HTTP error status and a JSON body that has a stable error code you can switch on.

The error shape

Every error has the same shape. code is stable and meant for your code. message is a sentence for people and may change. fields appears only for validation errors.

A validation error (400)
JSON
{
  "error": {
    "code": "validation",
    "message": "Check the highlighted fields and try again.",
    "fields": [
      { "path": "firstName", "message": "Required" },
      { "path": "email", "message": "Invalid email" }
    ]
  }
}

A path in fields names the part of the body that was wrong. For an id in the address that is not 24 characters of hexadecimal, the path is empty and the message is Invalid id.

Error codes

Error codes
HTTP statusCodeWhen it happens
400validationA field is missing or not valid, a query parameter is out of range, or an id is malformed. Read fields for what to fix.
400passwordRequiredYou handed a new organization or clinic to a person who does not have an account yet, without giving a password.
400pickClinicYou tried to create a patient, provider or order while working in an organization. Choose a clinic first with x-clinic-id.
400clinicRequiredA super admin created a patient, provider or order without choosing a clinic.
400parentMustBeOrganizationYou tried to create something under a clinic. Only an organization can contain sub-organizations or clinics.
400offerNotFoundAn order line names a SKU that is not in the catalog.
401unauthenticatedThere is no session, or it is invalid or expired. Sign in.
401invalidCredentialsThe email or password is not right.
403forbiddenYour role is not allowed to do this, or you may not create organizations.
403accountDisabledThe account has been disabled.
404notFoundThere is nothing at this address, or the item exists outside your scope or environment.
404patientNotFoundThe patient of an order is not in the clinic you are working in.
409emailTakenAn account with this email already exists.
409alreadyCancelledThe order was already cancelled.
500serverSomething went wrong on the HomelyRx side. Try again.

Handling errors

  • Switch on error.code, not on the message text.
  • On 401, sign in again and repeat the call once.
  • On 400 with validation, show or log fields. Do not retry the same body.
  • On 404, check the id, the clinic you are working in (x-clinic-id) and the environment (x-hrx-env).
  • On 500, retry after a short wait, and contact HomelyRx if it keeps happening.
const res = await fetch(`${API}/v1/orders/122703/cancel`, { method: 'POST', credentials: 'include' });
if (!res.ok) {
  const { error } = await res.json();
  if (error.code === 'alreadyCancelled') console.log('Nothing to do');
  else throw new Error(`${error.code}: ${error.message}`);
}