How the REST API works
Morphic exposes every model over a REST API under one base address. A client reads and writes entries with plain HTTP requests and receives JSON.
The base address is the morphic/v1 namespace on your WordPress site, for example https://example.com/wp-json/morphic/v1/. The API & integration screen shows this base address. Each model has its own paths below the base, such as items/{model} for lists and items/{model}/{id} for one entry.
Why it matters
Knowing who can read and write tells you whether a request needs a key before you send it. Knowing how lists page and how errors look lets a client handle large results and failures without guessing.
Who can read and write
- Public models can be read without logging in. A model is public when its read access includes
public. The API & integration screen labels such a model Public read. A model that needs a login shows Login needed to read. - Writes need an administrator request without a key, or an API key with write access. An anonymous write returns
403with the messageyou may not create {model}. - Administrators can read and write every model when the request sends no key. A key's limits apply to administrators too.
- A key can only use the models it was created for. A key without write access can read but not write.
- A malformed or unknown key is treated as no key. The request then follows the anonymous rules above.
To send a key, use either the X-Morphic-Key header or the Authorization: Bearer mk_… header.
Reading lists
- A list returns up to
limitentries. The default is 100. The maximum is 500. A value outside that range returns422with the messagelimit is 1 to 500. - The response includes a
nextvalue. Send it back in thecursorquery parameter to get the next page. On the last page,nextisnull. - Add
reverse=1to a list request to return its entries in reverse order.
Searching
Search needs a q query parameter. A search without it returns 422 with the message search needs q. The optional field parameter limits the search to one field, and lang selects a language.
Caching with ETag
Every successful read sends an ETag header and Cache-Control: private, no-cache. The ETag depends on the response and on the signed-in user, so two users can get different ETags for the same URL.
Send the ETag back in an If-None-Match header. If the response has not changed, the API returns 304 with no body.
Errors
Failed requests return a JSON body with an error object:
code: the failure type, such asforbidden,not_foundorinvalid.message: a short text for people reading the response.fieldandreason: present for validation failures, such asfield: "q"withreason: "required".retry:truefor failures that can succeed later, such asconflict,closed,unavailable,quota,index_buildingandbackpressure. Otherwisefalse.
The status code follows the failure type. forbidden returns 403, not_found returns 404, invalid returns 422, conflict returns 409, quota returns 429, and the unavailable types return 503. Any other error returns 500.
For the full list of codes, see Error codes and messages reference.
In practice
- To make a first read and write, follow Read and write content with the REST API.
- To create a key for a write, follow Create and revoke API keys.