REST API Companies - edmozley/freeitsm GitHub Wiki

๐Ÿข REST API: Companies

The usage guide for companies in the REST API - listing them, and creating and keeping them in step from an ERP or CRM. Mirrors the interactive documentation at System โ†’ API โ†’ Documentation. User guide: Multi-company ยท Developer guide: Companies.

[!NOTE] New to the API? The basics table on REST API: Tickets covers base URL, authentication, the response envelope, error codes, rate limits and PATCH semantics - identical across all modules.

What's distinctive about companies (5.3.0, #279):

๐Ÿ”‘ An outside reference external_ref is the company's ID in your ERP or CRM - unique when set, matched ignoring case. tax_id (VAT, AFM ...) is unique too.
๐Ÿ”„ Upsert by reference POST /companies/upsert creates the company the first time and updates it after - send the whole customer list every night and nothing is duplicated.
๐Ÿ›ก๏ธ Writes are an administrator's Creating, updating and upserting need a key that acts as a System administrator and is scoped to all companies - as System โ†’ Companies is administrators only. Reads follow the key's company scope as everywhere.

๐Ÿš€ Quick start

B="https://your-server/api/v1"; K="Authorization: Bearer fitsm_โ€ฆ"

# Find a company by its ERP reference (inactive ones too)
curl -s -H "$K" "$B/companies?external_ref=CUST-1042" | jq '.data[0]'

# Nightly: create or update each customer by its reference
curl -s -H "$K" -H "Content-Type: application/json" -X POST "$B/companies/upsert" -d '{
  "external_ref": "CUST-1042",
  "name": "Papadopoulos Bakery",
  "tax_id": "EL094019245",
  "is_active": true
}'
# 201 + "created": true the first time, 200 + "created": false after

# Change one field - only what is sent changes
curl -s -H "$K" -H "Content-Type: application/json" -X PATCH "$B/companies/12" -d '{"tax_id": "EL094019246"}'

๐Ÿ“š Endpoints

Method Path Permission What it does
GET /companies companies.read The companies the key can see: id, name, is_default, is_active, ticket_code, external_ref, tax_id. Filters q (name, reference or tax ID contains), external_ref and tax_id (exact, ignoring case), active (true - the default - false or all). An exact external_ref / tax_id lookup finds inactive companies too.
GET /companies/{id} companies.read One company; outside the key's scope is the same 404 as unknown.
POST /companies companies.create Create. name required; is_active, ticket_code, external_ref, tax_id optional. 201.
PATCH /companies/{id} companies.update Change only the fields sent; "" clears ticket_code, external_ref or tax_id.
POST /companies/upsert companies.create + update Create or update by external_ref (required). Returns the company and created.

Errors: 409 duplicate when a ticket code, outside reference or tax ID is already another company's (the message names it); 422 when the name is missing, a value is too long, or the Default company would be made inactive; 403 when the key is not an all-companies administrator's.

๐Ÿงฉ Custom fields

Every company and person carries custom_fields (5.4.0) - the fields an administrator makes up in System โ†’ Custom fields, each key with its value or null. They come back on the list, a single record and after every write, and are written the same way: on POST, PATCH and POST /companies/upsert for companies, POST and PATCH /users for people.

curl -s -H "$K" -H "Content-Type: application/json" -X POST "$B/companies/upsert" \n  -d '{"external_ref": "CUST-00042", "name": "Acme Plumbing Ltd", "custom_fields": {"support_tier": "Gold"}}'

"" or null clears a value. An unknown key, or a value the field does not accept (a choice not offered, an email that is not one), is refused with 422 and nothing is saved; a required field must be there when creating. A person's fields are their company's: move them with tenant_id and send that company's keys. See Custom fields on companies and people.

๐Ÿ‘ค People: employee ID

GET /users, GET /users/{id}, POST /users and PATCH /users/{id} carry employee_id (up to 64 characters, not unique), and GET /users?employee_id= finds a person by it - so an HR or ERP system can match its people to FreeITSM's.

๐Ÿ“จ How mail reaches a synced company

A person created with tenant_id set (POST /users) is that company's: mail from their address files with it even on a Gmail address, once nothing more specific (a company's specific senders, a mapped domain) claims it. See Multi-company: email routing.