JavaScript (and TypeScript) client for the CiviCRM API.
npm install civicrm-api
import { createClient } from "civicrm-api";
const client = createClient({
baseUrl: "https://example.com/civicrm",
auth: { apiKey: "<your-api-key>" },
entities: { contact: "Contact" },
});
const contactRequest = client.contact.get({ where: [["id", "=", "1"]] }).one();
You can optionally create an API v3 client by providing the relevant configuration:
const client = createClient({
// ...
api3: {
enabled: true,
entities: {
contact: {
name: "Contact",
actions: {
getList: "getlist",
},
},
},
});
const contactsRequest = client.contact.getList();
Configure a CiviCRM API client.
The base URL of the CiviCRM installation.
The API key, JWT, or username and password that will be used to authenticate requests. Refer to the CiviCRM authentication documentation.
const client = createClient({
// ...
auth: { apiKey: "api-key" },
//=> X-Civi-Auth: Bearer api-key
auth: { jwt: "jwt" },
//=> X-Civi-Auth: Bearer jwt
auth: { username: "user", password: "pass" },
//=> X-Civi-Auth: Basic dXNlcjpwYXNz
});
An object containing entities the client will be used to make requests for. Keys will be used to reference the entity within the client, and values should match the entity in CiviCRM:
const client = createClient({
// ...
entities: {
contact: "Contact",
activity: "Activity",
},
});
Set default request options for all requests.
Accepts
the same options as fetch
.
Headers will be merged with the default headers.
Enable logging request and response details to the console.
Enable API v3 client.
const client = createClient({
// ...
api3: {
enabled: true,
},
});
An object containing entities and actions the API v3 client will be used to make requests for. Keys will be used to reference the entity within the client. The value contains the name of the entity in API v3, and an object of actions, where the key is used to reference the action within the client, and the value is the action in API v3.
const client = createClient({
// ...
api3: {
enabled: true,
entities: {
contact: {
name: "Contact",
actions: {
getList: "getlist",
},
},
},
},
});
Create a request builder for a configured entity.
Request builders are used to build and execute requests.
Methods can be chained, and the request is executed by
calling .then()
or starting a chain with await
.
// Using .then()
client.contact
.get({ where: [["id", "=", "1"]] })
.one()
.then((contact) => {
//
});
// Using await
const contact = await client.contact.get({ where: [["id", "=", "1"]] }).one();
Set the action for the request to the method name, and optionally set request parameters.
An object accepting parameters accepted by CiviCRM
including select
, where
, having
, join
,
groupBy
, orderBy
, limit
, offset
and values
.
Alternatively accepts a key-value object for methods like getChecksum
.
Return a single record (i.e. set the index of the request to 0).
Chain a request for another entity within the current API call.
const contact = await client.contact
.get({ where: [["id", "=", "1"]] })
.chain(
"activity",
client.activity.get({ where: { target_contact_id: "$id" } }),
)
.one();
console.log(contact);
// => { id: 1, activity: [{ target_contact_id: 1, ... }], ... }
The label for the chained request, which will be used access the result of the chained request within the response.
A request builder for the chained request.
Set request options.
Accepts
the same options as fetch
.
Headers will be merged with the default headers.
client.contact.get().requestOptions({
headers: {
"X-Custom-Header": "value",
},
cache: "no-cache",
});
Create an API v3 request builder for a configured entity.
Request builders are used to build and execute requests.
Methods can be chained, and the request is executed by
calling .then()
or starting a chain with await
.
// Using .then()
client.api3.contact.getList({ input: "example" }).then((contacts) => {
//
});
// Using await
const contacts = await client.getList({ input: "example" });
Set the action for the request, and optionally set request parameters.
Set API options.
client.api3.contact.getList().option("limit", 10);
Set request options.
Accepts
the same options as fetch
.
Headers will be merged with the default headers.
client.api3.contact.getList().requestOptions({
headers: {
"X-Custom-Header": "value",
},
cache: "no-cache",
});
- The civicrm package from Tech to The People offers a different approach to building requests.
npm install
npm test
npm run build
-
Increment the version number and create a tag:
npm version <major|minor|patch|prerelease>
-
Push the tag to GitHub:
git push --tags
-
Create a release on GitHub. The package will be built and published to npm automatically by GitHub Actions.