Skip to content
Snippets Groups Projects
Unverified Commit 9de6e909 authored by colemanw's avatar colemanw Committed by GitHub
Browse files

Apply suggestions from code review


Co-Authored-By: default avatarMikey O'Toole <mikey@mjco.uk>
parent cb9d9b26
Branches
No related tags found
No related merge requests found
# The CiviCRM API
CiviCRM has a stable, comprehensive **API** (Application Programming Interface) that can be used to access and manage data in CiviCRM. The API is the recommended way for any CiviCRM extension, CMS module, or external program to interact with CiviCRM.
CiviCRM has a stable, comprehensive **API** (Application Programming Interface) that can be used to access and manage data in CiviCRM. The API is the recommended way for any CiviCRM extension, CMS module or external program to interact with CiviCRM.
Utilizing the API is superior to accessing core functions directly (e.g.calling raw SQL, or calling functions within the BAO files) because the API offers a consistent interface to CiviCRM's features. It is designed to function predictably with every new release so as to preserve backwards compatibility of the API for several versions of CiviCRM. If you decide to use other ways to collect data (like your own SQL statements), you risk running into future problems when changes to the schema and BAO arguments inevitably occur.
......@@ -8,15 +8,15 @@ The best place to begin working with the API is your own *test* install of CiviC
Extensions can provide additional API functionality. For help creating your own additions to the API, see [civix generate:api](/extensions/civix.md#generate-api).
## API versions
## API Versions
CiviCRM's API has major versions (APIv2, APIv3, APIv4) which are independent of the CiviCRM version. The API version is incremented more slowly in order to maintain stability within the extension ecosystem. Typically, two versions of the API are maintained concurrently (currently v3 and v4) to allow gradual transitions. New releases of CiviCRM may add features to the API but will not break backward-compatibility within an API version.
## API explorer
## API Explorer
The API explorer is a powerful GUI tool for building and executing API calls. To access it:
1. Log in to a CiviCRM site as an administrator
1. Log in to a CiviCRM site as an administrator.
* This can even be the [demo site](http://dmaster.demo.civicrm.org/).
2. Within the CivCRM menu, go to **Support > Developer** and either **API Explorer v3** or **API Explorer v4** (URL `/civicrm/api` or `/civicrm/api4`).
......@@ -29,7 +29,7 @@ You can select the entity you want to use, for example `Contact` and the action
From the API explorer, you can get documentation on how to construct your API query. This is done either in the screen as you fill out the GUI to create your API call or in the v3 Explorer there are docs under the Code Docs tab which will point at the relevant aspects of the v3 code base that run the API calls.
## API examples (v3 only)
## API Examples (APIv3 Only)
Within the API Explorer you will be able to attain an example of the code that you should write to call the API. In APIv3, you can also access epscific examples of some API calls from the Examples tab within the explorer. You can also [explore these examples on GitHub](https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples).
......
# API v4 Changelog
This page lists additions to the v4 api with each new release of CiviCRM core.
This page lists additions to the v4 api with each new release of CiviCRM Core.
Also see: [Differences between Api v3 and v4](/api/v4/differences-with-v3.md).
## Nothing here yet.
Api v4 is brand new. No changes yet!
API v4 is brand new. No changes yet!
# Differences between API v3 and v4
# Differences Between API v3 and v4
APIv4 is broadly similar to APIv3. Both are designed for reading and writing data.
Both use *entities*, *actions*, and *parameters*. However, APIv4 is specifically a
......@@ -76,36 +76,36 @@ APIv4:
</table>
## API wrapper
## API Wrapper
- APIv4 supports two notations in PHP:
- Procedural/array style: `civicrm_api4('Entity', 'action', $params)`
- Object-oriented style: `\Civi\Api4\Entity::action()->...->execute()`
- When using OOP style in an IDE, most actions and parameters can benefit from auto-completion and type-checking.
- `$checkPermissions` always defaults to `TRUE`. In APIv3, the default depended on the environment (`TRUE` in REST/Javascript; `FALSE` in PHP).
- A 4th param `index` controls how results are returned:
- Passing a string will index all results by that key e.g. `civicrm_api4('Contact', 'get', $params, 'id')` will index by id.
- Passing a number will return the result at that index e.g. `civicrm_api4('Contact', 'get', $params, 0)` will return the first result and is the same as `\Civi\Api4\Contact::get()->execute()->first()`. `-1` is the equivalent of `$result->last()`.
- When chaining API calls together, back-references to values from the main API call must be explicitly given (discoverable in the API Explorer).
* APIv4 supports two notations in PHP:
* Procedural/array style: `civicrm_api4('Entity', 'action', $params)`
* Object-oriented style: `\Civi\Api4\Entity::action()->...->execute()`
* When using OOP style in an IDE, most actions and parameters can benefit from auto-completion and type-checking.
* `$checkPermissions` always defaults to `TRUE`. In APIv3, the default depended on the environment (`TRUE` in REST/Javascript; `FALSE` in PHP).
* A 4th param `index` controls how results are returned:
* Passing a string will index all results by that key e.g. `civicrm_api4('Contact', 'get', $params, 'id')` will index by id.
* Passing a number will return the result at that index e.g. `civicrm_api4('Contact', 'get', $params, 0)` will return the first result and is the same as `\Civi\Api4\Contact::get()->execute()->first()`. `-1` is the equivalent of `$result->last()`.
* When chaining API calls together, back-references to values from the main API call must be explicitly given (discoverable in the API Explorer).
## Actions
- For `Get`, the default `limit` has changed. If you send an API call without an explicit limit, then it will return *all* records. (In v3, it would silently apply a default of 25.) However, if you use the API Explorer, it will *recommend* a default limit of 25.
- The `Create` action is now only used for creating *new* items (no more implicit update by passing an id to v3 `create`).
- The `Save` action in v4 is most similar to v3's `create` - it accepts one or more records to create or update, infering the action based on the presence of `id` in each record.
- `Update` and `Delete` can be performed on multiple items at once by specifying a `where` clause, vs a single item by id in v3.
- `getsingle` is gone, use `$result->first()` or `index` `0`.
- `getoptions` is no longer a standalone action, but part of `getFields`.
* For `Get`, the default `limit` has changed. If you send an API call without an explicit limit, then it will return *all* records. (In v3, it would silently apply a default of 25.) However, if you use the API Explorer, it will *recommend* a default limit of 25.
* The `Create` action is now only used for creating *new* items (no more implicit update by passing an id to v3 `create`).
* The `Save` action in v4 is most similar to v3's `create` - it accepts one or more records to create or update, infering the action based on the presence of `id` in each record.
* `Update` and `Delete` can be performed on multiple items at once by specifying a `where` clause, vs a single item by id in v3.
* `getsingle` is gone, use `$result->first()` or `index` `0`.
* `getoptions` is no longer a standalone action, but part of `getFields`.
## Output
- Output is an object (`Result` aka [`ArrayObject`](https://www.php.net/manual/en/class.arrayobject.php)) rather than a plain `array`.
- In PHP, you can iterate over the `ArrayObject` (`foreach ($myResult as $record)`), or you can call methods like `$result->first()` or `$result->indexBy('foo')`.
- By default, results are indexed sequentially (`0,1,2,3,...` - like APIv3's `sequential => 1`). You may optionally index by `id`, `name`, or any other field, as in:
- (Procedural-style; use `$index` parameter): `civicrm_api4('Contact', 'get', [], 'id')`
- (OOP-style; use `indexBy()` method): `\Civi\Api4\Contact::get()->execute()->indexBy('id')`
* Output is an object (`Result` aka [`ArrayObject`](https://www.php.net/manual/en/class.arrayobject.php)) rather than a plain `array`.
* In PHP, you can iterate over the `ArrayObject` (`foreach ($myResult as $record)`), or you can call methods like `$result->first()` or `$result->indexBy('foo')`.
* By default, results are indexed sequentially (`0,1,2,3,...` - like APIv3's `sequential => 1`). You may optionally index by `id`, `name`, or any other field, as in:
* (Procedural-style; use `$index` parameter): `civicrm_api4('Contact', 'get', [], 'id')`
* (OOP-style; use `indexBy()` method): `\Civi\Api4\Contact::get()->execute()->indexBy('id')`
## Input
- Instead of a single `$params` array containing a mishmash of fields, options and parameters, each APIv4 parameter is distinct (see section on Params below).
- Custom fields are refered to by name rather than id. E.g. use `constituent_information.Most_Important_Issue` instead of `custom_4`.
* Instead of a single `$params` array containing a mishmash of fields, options and parameters, each APIv4 parameter is distinct (see section on Params below).
* Custom fields are refered to by name rather than id. E.g. use `constituent_information.Most_Important_Issue` instead of `custom_4`.
## Params
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment