<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rfc SYSTEM 'rfc2629.dtd' []>
<rfc ipr="trust200902" category="std" docName="draft-ietf-jmap-core-04">
<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc private=""?>
<?rfc topblock="yes"?>
<?rfc comments="no"?>
<front>
<title abbrev="JMAP">JSON Meta Application Protocol</title>

<author initials="N.M." surname="Jenkins" fullname="Neil Jenkins">
<organization>FastMail</organization>
<address>
<postal>
<street>Level 2, 114 William St</street>
<city>Melbourne</city>
<code>VIC 3000</code>
<country>Australia</country>
<region></region>
</postal>
<phone></phone>
<email>neilj@fastmailteam.com</email>
<uri>https://www.fastmail.com</uri>
</address>
</author>
<date year="2018" month="March" day="5"/>

<area>Applications</area>
<workgroup>JMAP</workgroup>
<keyword>JMAP</keyword>
<keyword>JSON</keyword>


<abstract>
<t>This document specifies a protocol for synchronising JSON-based data objects efficiently, with support for push and out-of-band binary data upload/download.
</t>
</abstract>


</front>

<middle>

<section anchor="introduction" title="Introduction">
<t>JMAP is a generic protocol for synchronising data, such as mail, calendars or contacts, between a client and a server. It is optimised for mobile and web environments, and aims to provide a consistent interface to different data types.
</t>
<t>This specification is for the generic mechanism of data synchronisation. Further specifications define the data models for different data types that may be synchronised via JMAP.
</t>
<t>JMAP is designed to make efficient use of limited network resources. Multiple API calls may be batched in a single request to the server, reducing round trips and improving battery life on mobile devices. Push connections remove the need for polling, and an efficient delta update mechanism ensures a minimum of data is transferred.
</t>
<t>JMAP is designed to be horizontally scalable to a very large number of users. This is facilitated by the separate end points for users after login, the separation of binary and structured data, and a shared data model that does not allow data dependencies between accounts.
</t>

<section anchor="notational-conventions" title="Notational conventions">
<t>The key words &quot;MUST&quot;, &quot;MUST NOT&quot;, &quot;REQUIRED&quot;, &quot;SHALL&quot;, &quot;SHALL NOT&quot;, &quot;SHOULD&quot;, &quot;SHOULD NOT&quot;, &quot;RECOMMENDED&quot;, &quot;MAY&quot;, and &quot;OPTIONAL&quot; in this document are to be interpreted as described in <xref target="RFC2119"/>.
</t>
<t>The underlying format used for this specification is JSON. Consequently, the terms &quot;object&quot; and &quot;array&quot; as well as the four primitive types (strings, numbers, booleans, and null) are to be interpreted as described in Section 1 of <xref target="RFC7159"/>. Unless otherwise noted, all the property names and values are case sensitive.
</t>
<t>Some examples in this document contain &quot;partial&quot; JSON documents used for illustrative purposes.  In these examples, three periods &quot;...&quot; are used to indicate a portion of the document that has been removed for compactness.
</t>
<t>Types signatures are given for all JSON objects in this document. The following conventions are used:
</t>
<t>
<list style="symbols">
<t><spanx style="verb">Boolean|String</spanx> – The value is either a JSON <spanx style="verb">Boolean</spanx> value, or a JSON <spanx style="verb">String</spanx> value.</t>
<t><spanx style="verb">Foo</spanx> – Any name that is not a native JSON type means an object for which the properties (and their types) are defined elsewhere within this document.</t>
<t><spanx style="verb">Foo[]</spanx> – An array of objects of type <spanx style="verb">Foo</spanx>.</t>
<t><spanx style="verb">String[Foo]</spanx> – A JSON <spanx style="verb">Object</spanx> being used as a map (associative array), where all the values are of type <spanx style="verb">Foo</spanx>.</t>
</list>
</t>
</section>

<section anchor="the-number-datatype" title="The Number datatype">
<t>The JSON datatypes are limited to those found in JavaScript. A <spanx style="verb">Number</spanx> in JavaScript is represented as a signed double (64-bit floating point). However, except where explicitly specified, all numbers used in this API are unsigned integers &lt;= 2^53 (the maximum integer that may be reliably stored in a double).
</t>
</section>

<section anchor="the-date-datatypes" title="The Date datatypes">
<t>Where <spanx style="verb">Date</spanx> is given as a type, it means a string in <xref target="RFC3339"/> <spanx style="emph">date-time</spanx> format. To ensure a normalised form, the <spanx style="emph">time-secfrac</spanx> MUST always  be omitted and any letters in the string (e.g. &quot;T&quot; and &quot;Z&quot;) MUST be upper-case. For example, <spanx style="verb">"2014-10-30T14:12:00+08:00"</spanx>.
</t>
<t>Where <spanx style="verb">UTCDate</spanx> is given as a type, it means a <spanx style="verb">Date</spanx> where the <spanx style="emph">time-offset</spanx> component MUST be <spanx style="verb">Z</spanx> (i.e. it must be in UTC time). For example, <spanx style="verb">"2014-10-30T06:12:00Z"</spanx>.
</t>
</section>

<section anchor="json-as-the-data-encoding-format" title="JSON as the data encoding format">
<t>JSON is a text-based data interchange format as specified in <xref target="RFC7159"/>. The I-JSON format defined in <xref target="RFC7493"/> is a strict subset of this, adding restrictions to avoid potentially confusing scenarios (for example, it mandates that an object MUST NOT have two properties with the same key).
</t>
<t>All data sent from the client to the server or from the server to the client (except binary file upload/download) MUST be valid I-JSON according to the RFC, and is therefore case-sensitive and encoded in UTF-8 (<xref target="RFC3629"/>).
</t>
</section>

<section anchor="terminology" title="Terminology">

<section anchor="user" title="User">
<t>A user represents a set of permissions relating to what data can be seen.
</t>
</section>

<section anchor="accounts" title="Accounts">
<t>An account is a collection of data. A single account may contain an arbitrary set of data types, for example a collection of mail, contacts and calendars. Most operations in JMAP are isolated to a single account; there are a few explicit operations to copy data between them. Certain properties are guaranteed for data within the same account, for example uniqueness of ids within a type in that account.
</t>
<t>An account is not the same as a user, although it is common for the primary account to directly belong to the user. For example, you may have an account that contains data for a group or business, to which multiple users have access. Users may also have access to accounts belonging to another user if that user is sharing some of their data. A single set of credentials may provide access to data in multiple accounts.
</t>
</section>

<section anchor="data-types-and-records" title="Data types and records">
<t>JMAP provides a uniform interface for creating, retrieving, updating and deleting various types of objects. A <spanx style="strong">data type</spanx> is a collection of named, typed properties, just like the schema for a database table. Each instance of a data type is called a <spanx style="strong">record</spanx>.
</t>
</section>
</section>

<section anchor="ids" title="Ids">
<t>All record ids are assigned by the server, and are immutable. They MUST be unique among all records of the <spanx style="strong">same type</spanx> within the <spanx style="strong">same account</spanx>. Ids may clash across accounts, or for two records of different types within the same account.
</t>
<t>Ids are always <spanx style="verb">String</spanx>s. An id MUST be a valid UTF-8 string of at least 1 character in length and maximum 256 octets in size, but MUST NOT start with the <spanx style="verb">#</spanx> character, as this is reserved for doing back references during object creation (see the <spanx style="emph">/set</spanx> description).
</t>
</section>

<section anchor="the-jmap-api-model" title="The JMAP API model">
<t>JMAP uses HTTP <xref target="RFC7230"/> to expose API, Push, Upload and Download resources. Implementations MUST support HTTP/1.1, and MAY support later versions. Support for common HTTP mechanisms such as redirection and caching are assumed.
</t>
<t>All HTTP requests MUST be authenticated. Servers MUST conform with the <xref target="RFC7235"/> HTTP Authentication framework to reject requests that fail authentication and inform the client of available authentication schemes.
</t>
<t>Clients SHOULD understand and be able to handle standard HTTP status codes appropriately.
</t>
<t>An authenticated client can fetch the JMAP Session object with details about the data and capabilities the server can provide as shown in section 2. The client may then exchange data with the server in the following ways:
</t>
<t>
<list style="numbers">
<t>The client may make an API request to the server to get or set structured data. This request consists of an ordered series of method calls. These are processed by the server, which then returns an ordered series of responses. This is described in sections 3 and 4.</t>
<t>The client may download or upload binary files from/to the server. This is detailed in section 5.</t>
<t>The client may connect to a push channel on the server, to be notified when data has changed. This is explained in section 6.</t>
</list>
</t>
</section>
</section>

<section anchor="the-jmap-session-resource" title="The JMAP Session resource">
<t>To communicate with a JMAP server you need two things to start:
</t>
<t>
<list style="numbers">
<t>The URL for the JMAP Session resource. This may be requested directly from
the user, or discovered automatically based on a username domain (see Service Autodiscovery section below).</t>
<t>Credentials to authenticate with. How to obtain credentials is out of scope
for this specification.</t>
</list>
</t>
<t>An authenticated GET request to the JMAP Session resource MUST return the details about the data and capabilities the server can provide to the client given those credentials.
</t>
<t>The response to a successful request is a JSON-encoded <spanx style="strong">JMAP Session</spanx> object. It has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">username</spanx>: <spanx style="verb">String</spanx>
The username associated with the given credentials.</t>
<t><spanx style="strong">accounts</spanx>: <spanx style="verb">String[Account]</spanx>
A map of <spanx style="strong">account id</spanx> to Account object for each account the user has access to.
A single set of credentials may provide access to multiple accounts, for example if another user is sharing their mail with the logged in user, or if there is an account that contains data for a group or business.
All data belongs to a single account. With the exception of a few explicit operations to copy data between accounts, all JMAP methods take an <spanx style="emph">accountId</spanx> argument that specifies on which account the operations are to take place. This argument is always optional; if not specified, the primary account is used. All ids (other than account ids of course) are only unique within their account.
In the event of a severe internal error, a server may have to reallocate ids or do something else that violates standard JMAP data constraints. In this situation, the data on the server is no longer compatible with cached data the client may have from before. The server MUST treat this as though the account has been deleted and then recreated with a new account id. Clients will then be forced to throw away any data with the old account id and refetch all data from scratch.
An <spanx style="strong">Account</spanx> object has the following properties:
<list style="symbols">
<t><spanx style="strong">name</spanx>: <spanx style="verb">String</spanx>
A user-friendly string to show when presenting content from this account, e.g. the email address representing the owner of the account.</t>
<t><spanx style="strong">isPrimary</spanx>: <spanx style="verb">Boolean</spanx>
This MUST be true for <spanx style="strong">at most</spanx> one of the accounts returned. This is to be considered the user's main or default account by the client. If no account being returned belongs to the user, or in any other way there is no appropriate way to determine a default account, then this MAY be <spanx style="verb">false</spanx> for all accounts.</t>
<t><spanx style="strong">isReadOnly</spanx>: <spanx style="verb">Boolean</spanx>
This is <spanx style="verb">true</spanx> if the entire account is read-only.</t>
<t><spanx style="strong">hasDataFor</spanx>: <spanx style="verb">String[]</spanx>
A list of the data profiles available in this account. Each future JMAP data types specification will define a profile name to encompass that set of types.</t>
</list></t>
<t><spanx style="strong">capabilities</spanx>: <spanx style="verb">String[Object]</spanx>
An object specifying the capabilities of this server. Each key is a URI for a specification supported by the server. The value for each of these keys is an object with further information about the server's capabilities in relation to that specification.
The client MUST ignore any properties it does not understand.
The capabilities object MUST include a property called <spanx style="verb">ietf:jmap</spanx>. The value of this property is an object which MUST contain the following information on server capabilities:
<list style="symbols">
<t><spanx style="strong">maxSizeUpload</spanx>: <spanx style="verb">Number</spanx>
The maximum file size, in octets, that the server will accept for a single file upload (for any purpose).</t>
<t><spanx style="strong">maxConcurrentUpload</spanx>: <spanx style="verb">Number</spanx>
The maximum number of concurrent requests the server will accept to the upload endpoint.</t>
<t><spanx style="strong">maxSizeRequest</spanx>: <spanx style="verb">Number</spanx>
The maximum size, in octets, that the server will accept for a single
request to the API endpoint.</t>
<t><spanx style="strong">maxConcurrentRequests</spanx>: <spanx style="verb">Number</spanx>
The maximum number of concurrent requests the server will accept to
the API endpoint.</t>
<t><spanx style="strong">maxCallsInRequest</spanx>: <spanx style="verb">Number</spanx>
The maximum number of method calls the server will accept in a single request to the API endpoint. This MUST be greater than or equal to <spanx style="verb">32</spanx> to ensure clients can rely on the ability to make efficient network use.</t>
<t><spanx style="strong">maxObjectsInGet</spanx>: <spanx style="verb">Number</spanx>
The maximum number of objects that the client may request in a single <spanx style="verb">/get</spanx> type method call.</t>
<t><spanx style="strong">maxObjectsInSet</spanx>: <spanx style="verb">Number</spanx>
The maximum number of objects the client may send to create, update or destroy in a single <spanx style="verb">/set</spanx> type method call.</t>
<t><spanx style="strong">collationAlgorithms</spanx>: <spanx style="verb">String[]</spanx>
A list of identifiers for algorithms registered in the collation registry defined in <xref target="RFC4790"/> that the server supports for sorting when querying records.</t>
</list>
Future specifications will define their own properties on the capabilities object.
Servers MAY advertise vendor-specific JMAP extensions. To avoid conflict, the identifiers for these MUST be a URI beginning with a domain owned by the vendor. Clients MUST opt in to any specifications it wishes to use (see &quot;Making an API request&quot;).</t>
<t><spanx style="strong">apiUrl</spanx>: <spanx style="verb">String</spanx>
The URL to use for JMAP API requests.</t>
<t><spanx style="strong">downloadUrl</spanx>: <spanx style="verb">String</spanx>
The URL endpoint to use when downloading files (see the Download section of this spec), in <xref target="RFC6570"/> URI Template (level 1) format. The URL MUST contain variables called <spanx style="verb">blobId</spanx>, MAY contain a variables called <spanx style="verb">accountId</spanx> and SHOULD contain a variable called <spanx style="verb">name</spanx>.</t>
<t><spanx style="strong">uploadUrl</spanx>: <spanx style="verb">String</spanx>
The URL endpoint to use when uploading files (see the Upload section of this spec), in <xref target="RFC6570"/> URI Template (level 1) format. The URL MAY contain a variable called <spanx style="verb">accountId</spanx>.</t>
<t><spanx style="strong">eventSourceUrl</spanx>: <spanx style="verb">String</spanx>
The URL to connect to for push events (see the Push section of this spec).</t>
</list>
</t>
<t>To ensure future compatibility, other properties MAY be included on the JMAP Session object. Clients MUST ignore any properties they are not expecting.
</t>

<section anchor="service-autodiscovery" title="Service Autodiscovery">
<t>There are two standardised autodiscovery methods in use for internet protocols:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">DNS srv</spanx> (<xref target="RFC6186"/> and <xref target="RFC6764"/>)</t>
<t><spanx style="strong">.well-known/servicename</spanx> (<xref target="RFC5785"/>)</t>
</list>
</t>
<t>A JMAP-supporting host for the domain <spanx style="verb">example.com</spanx> SHOULD publish a SRV record <spanx style="verb">_jmaps._tcp.example.com</spanx> which gives a <spanx style="emph">hostname</spanx> and <spanx style="emph">port</spanx> (usually port <spanx style="verb">443</spanx>). The JMAP Session resource is then <spanx style="verb">https://${hostname}[:${port}]/.well-known/jmap</spanx> (following any redirects).
</t>
<t>If the client has a username in the form of an email address, it MAY use the domain portion of this to attempt autodiscovery of the JMAP server.
</t>
</section>
</section>

<section anchor="structured-data-exchange" title="Structured data exchange">
<t>The client may make an API request to the server to get or set structured data. This request consists of an ordered series of method calls. These are processed by the server, which then returns an ordered series of responses.
</t>

<section anchor="making-an-api-request" title="Making an API request">
<t>To make an API request, the client makes an authenticated POST request to the API resource, the location of which may be found on the JMAP Session object.
</t>
<t>The request MUST consist of a single JSON <spanx style="strong">Request</spanx> object. If successful, the response MUST also be of type <spanx style="verb">application/json</spanx> and consist of a single <spanx style="strong">Response</spanx> object.
</t>
</section>

<section anchor="the-request-object" title="The Request object">
<t>A <spanx style="strong">Request</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">using</spanx>: <spanx style="verb">String[]</spanx>
The set of capabilities the client wishes to use. The client MAY include capability identifiers even if the method calls it makes do not utilise those capabilities.
The server advertises the set of specifications it supports in the JMAP Session object, as keys on the <spanx style="emph">capabilities</spanx> property.</t>
<t><spanx style="strong">methodCalls</spanx>: <spanx style="verb">Array[]</spanx>
An array of method calls to process on the server. The method calls MUST be processed sequentially, in order.
A <spanx style="strong">method call</spanx> is represented by an array containing three elements:
<list style="numbers">
<t>A <spanx style="verb">String</spanx> <spanx style="strong">name</spanx> of the method to call.</t>
<t>An <spanx style="verb">Object</spanx> containing <spanx style="emph">named</spanx> <spanx style="strong">arguments</spanx> for that method.</t>
<t>A <spanx style="strong">client id</spanx>: an arbitrary <spanx style="verb">String</spanx> to be echoed back with the responses emitted by that method call (a method may return 1 or more responses, as it may make implicit calls to other methods; all responses initiated by this method call get the same client id in the response).</t>
</list></t>
</list>
</t>
<t>Future specifications MAY add further properties to the Request object to extend the semantics. To ensure forwards compatability, a server MUST ignore any other properties it does not understand on the JMAP request object.
</t>

<section anchor="example-request" title="Example request">

<figure align="center"><artwork align="center">
{
  "using": [ "ietf.org/rfc/jmap-core", "ietf.org/rfc/jmap-mail" ],
  "methodCalls": [
    ["method1", {"arg1": "arg1data", "arg2": "arg2data"}, "#1"],
    ["method2", {"arg1": "arg1data"}, "#2"],
    ["method3", {}, "#3"]
  ]
}
</artwork></figure>
</section>
</section>

<section anchor="vendorspecific-extensions" title="Vendor-specific extensions">
<t>Individual services will have custom features they wish to expose over JMAP. This may take the form of extra datatypes and/or methods not in the spec, or extra arguments to JMAP methods, or extra properties on existing data types (which may also appear in arguments to methods that take property names).
</t>
<t>The server can advertise custom extensions it supports by including the identifiers in the capabilities object. Identifiers for vendor extensions MUST be a URL belonging to a domain owned by the vendor, to avoid conflict. The URL SHOULD resolve to documentation for the changes the extension makes.
</t>
<t>To ensure compatibility with clients that don't know about a specific custom extension, and for compatibility with future versions of JMAP, to use an extension the client MUST opt in by passing the appropriate capability identifier in the <spanx style="emph">using</spanx> array of the Request object. The server MUST only follow the specifications that are opted-into and behave as though it does not implement anything else when processing a request.
</t>
</section>

<section anchor="the-response-object" title="The Response object">
<t>A <spanx style="strong">Response</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">methodResponses</spanx>: <spanx style="verb">Array[]</spanx>
An array of responses, in the same format as the <spanx style="emph">methodCalls</spanx> on the request object. The output of the methods MUST be added to the <spanx style="emph">methodResponses</spanx> array in the same order as the methods are processed.</t>
</list>
</t>
<t>Unless otherwise specified, if the method call completed successfully its response name is the same as the method name in the request.
</t>

<section anchor="example-response" title="Example response:">

<figure align="center"><artwork align="center">
{
  "methodResponses": [
    ["method1", {"arg1": 3, "arg2": "foo"}, "#1"],
    ["method2", {"isBlah": true}, "#2"],
    ["anotherResponseFromMethod2", {
      "data": 10,
      "yetmoredata": "Hello"
    }, "#2"],
    ["error", {"type":"unknownMethod"}, "#3"]
  ]
}
</artwork></figure>
</section>
</section>

<section anchor="omitting-arguments" title="Omitting arguments">
<t>An argument to a method may be specified to have a default value. If omitted by the client, the server MUST treat the method call the same as if the default value had been specified. Similarly, the server MAY omit any argument in a response which has the default value.
</t>
<t>Unless otherwise specified in a method description, <spanx style="verb">null</spanx> is the default value for any argument in a request or response where this is allowed by the type signature. Other arguments may only be omitted if an explicit default value is defined in the method description.
</t>
</section>

<section anchor="errors" title="Errors">

<section anchor="requestlevel-errors" title="Request-level errors">
<t>If the data sent as an API request is not valid JSON or does not match the structure above, or includes a capability that the server does not support in the &quot;using&quot; property of the request, a <spanx style="verb">400 Bad Request</spanx> error will be returned at the HTTP level. The body of the response SHOULD include a short description of the problem to help client developers debug the issue.
</t>
</section>

<section anchor="methodlevel-errors" title="Method-level errors">
<t>If a method encounters an error, the appropriate <spanx style="verb">error</spanx> response MUST be inserted at the current point in the <spanx style="emph">methodResponses</spanx> array and, unless otherwise specified, further processing MUST NOT happen within that method call.
</t>
<t>Any further method calls in the request MUST then be processed as normal.
</t>
<t>An <spanx style="verb">error</spanx> response looks like this:
</t>

<figure align="center"><artwork align="center">
["error", {
  type: "unknownMethod"
}, "client-id"]
</artwork></figure>
<t>The response name is <spanx style="verb">error</spanx>, and it MUST have a type property. Other properties may be present with further information; these are detailed in the error type descriptions where appropriate.
</t>
<t>With the exception of <spanx style="verb">serverError</spanx>, the externally-visible state of the server MUST NOT have changed if an error is returned at the method level.
</t>
<t>The following error types are defined which may be returned for any method call where appropriate:
</t>
<t><spanx style="verb">serverError</spanx>: An unexpected or unknown error occured during the processing of the call. The state of the server after such an error is undefined.
</t>
<t><spanx style="verb">unknownMethod</spanx>: The server does not recognise this method name.
</t>
<t><spanx style="verb">invalidArguments</spanx>: One of the arguments is of the wrong type or otherwise invalid, or a required argument is missing. A <spanx style="verb">description</spanx> property MAY be present to help debug with an explanation of what the problem was. This is a non-localised string, and is not intended to be shown directly to end users.
</t>
<t><spanx style="verb">forbidden</spanx>: The method and arguments are valid, but executing the method would violate an ACL or other permissions policy.
</t>
<t><spanx style="verb">timedOut</spanx>: The method failed to execute because it timed out waiting for a lock, or was taking too much compute time.
</t>
<t><spanx style="verb">accountNotFound</spanx>: An <spanx style="emph">accountId</spanx> was included with the method call that does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: An <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">accountReadOnly</spanx>: This method call would modify state in an account that has <spanx style="verb">isReadOnly == true</spanx>.
</t>
<t>Further possible errors for a particular method are specified in the method descriptions.
</t>
<t>Further general errors MAY be defined in future RFCs. Should a client receive an error type it does not understand, it MUST treat it the same as the <spanx style="verb">serverError</spanx> type.
</t>
</section>
</section>

<section anchor="references-to-previous-method-results" title="References to previous method results">
<t>To allow clients to make more efficient use of the network and avoid round
trips, an argument to one method can be taken from the result of a previous
method call.
</t>
<t>To do this, the client prefixes the argument name with &quot;#&quot;. The value is a <spanx style="emph">ResultReference</spanx> object as described below. When processing a method call, the server MUST first check the arguments object for any names beginning with &quot;#&quot;. If found, the back reference should be resolved and the value used as the &quot;real&quot; argument. The method is then processed as normal. If any back reference fails to resolve, the whole method MUST be rejected with a <spanx style="verb">resultReference</spanx> error. If an argument object contains the same argument name in normal and referenced form (e.g. <spanx style="verb">foo</spanx> and <spanx style="verb">#foo</spanx>), the method MUST return an <spanx style="verb">invalidArguments</spanx> error.
</t>
<t>A <spanx style="strong">ResultReference</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">resultOf</spanx>: <spanx style="verb">String</spanx>
The client id of the method call to get the result from (the string given as the third item in the array for a method call).</t>
<t><spanx style="strong">name</spanx>: <spanx style="verb">String</spanx>
The expected name of the response.</t>
<t><spanx style="strong">path</spanx>: <spanx style="verb">String</spanx>
A pointer into the arguments. This is an RFC6901 JSON Pointer, except it also allows the use of <spanx style="verb">*</spanx> to map through an array (see description below).</t>
</list>
</t>
<t>To resolve:
</t>
<t>
<list style="numbers">
<t>Find the first response with a client id identical to the <spanx style="emph">resultOf</spanx>
property of the <spanx style="emph">ResultReference</spanx> in the <spanx style="emph">methodResponses</spanx> array from previously processed method calls in the same request. If none, evaluation fails.</t>
<t>If the response name is not identical to the <spanx style="emph">name</spanx> property of the
<spanx style="emph">ResultReference</spanx>, evaluation fails.</t>
<t>Apply the <spanx style="emph">path</spanx> to the arguments object of the response (the second item in
the response array) following the <xref target="RFC6901"/> JSON pointer algorithm, except with the following addition in Section 4 (Evaluation):</t>
</list>
</t>
<t>If the currently referenced value is a JSON array, the reference token may
   be exactly the single character <spanx style="verb">*</spanx>, making the new referenced value the result of applying the rest of the JSON pointer tokens to every item in the array and returning the results in the same order in a new array. If the result of applying the rest of the pointer tokens to a value was itself an array, its items should be included individually in the output rather than including the array itself (i.e. the result is flattened from an array of arrays to a single array).
</t>
<t>As a simple example, suppose we have the following API request <spanx style="emph">methodCalls</spanx>:
</t>

<figure align="center"><artwork align="center">
[[ "Foo/changes", {
    "sinceState": "abcdef"
}, "t0" ],
[ "Foo/get", {
    "#ids": {
        "resultOf": "t0",
        "name": "Foo/changes",
        "path": "/changed"
    }
}, "t1" ]]
</artwork></figure>
<t>After executing the first method call the <spanx style="emph">methodResponses</spanx> array is:
</t>

<figure align="center"><artwork align="center">
[[ "Foo/changes", {
    "accountId": "1",
    "oldState": "abcdef",
    "newState": "123456",
    "hasMoreChanges": false,
    "changed": [ "f1", "f4" ],
    "destroyed": []
}, "t0" ]]
</artwork></figure>
<t>So to execute the Foo/get call, we look through the arguments and find there is one with a <spanx style="verb">#</spanx> prefix. To resolve this, we apply the algorithm above:
</t>
<t>
<list style="numbers">
<t>Find the first response with client id &quot;t0&quot;. The Foo/changes response
fulfils this criterion.</t>
<t>Check the response name is the same as in the result reference. It is, so
this is fine.</t>
<t>Apply the <spanx style="emph">path</spanx> as a JSON pointer to the arguments object. This simply
selects the &quot;changed&quot; property, so the result of evaluating is:
<spanx style="verb">[ "f1", "f4" ]</spanx></t>
</list>
</t>
<t>The JMAP server now continues to process the Foo/get call as though the arguments were:
</t>

<figure align="center"><artwork align="center">
{
    "ids": [ "f1", "f4" ]
}
</artwork></figure>
<t>Now a more complicated example using the JMAP Mail data model: fetch the &quot;from&quot;/&quot;date&quot;/&quot;subject&quot; for every email in the first 10 threads in the Inbox (sorted newest first):
</t>

<figure align="center"><artwork align="center">
[[ "Email/query", {
  "filter": { inMailbox: "id_of_inbox" },
  "sort": [{ property: "receivedAt", isAscending: false }],
  "collapseThreads": true,
  "position": 0,
  "limit": 10
}, "t0" ],
[ "Email/get", {
  "#ids": {
    "resultOf": "t0",
    "name": "Email/query",
    "path": "/ids"
  },
  "properties": [ "threadId" ]
}, "t1" ],
[ "Thread/get", {
  "#ids": {
    "resultOf": "t1",
    "name": "Email/get",
    "path": "/list/*/threadId"
  }
}, "t2" ],
[ "Email/get", {
  "#ids": {
    "resultOf": "t2",
    "name": "Thread/get",
    "path": "/list/*/emailIds"
  },
  "properties": [ "from", "receivedAt", "subject" ]
}, "t3" ]]
</artwork></figure>
<t>After executing the first 3 method calls the <spanx style="emph">methodResponses</spanx> array might be:
</t>

<figure align="center"><artwork align="center">
[[ "Email/query", {
    "accountId": "1",
    "filter": { inMailbox: "id_of_inbox" },
    "sort": [{ property: "receivedAt", isAscending: false }],
    "collapseThreads": true,
    "state": "abcdefg",
    "canCalculateChanges": true,
    "position": 0,
    "total": 101,
    "ids": [ "msg1023", "msg223", "msg110", "msg93", "msg91", "msg38", "msg36", "msg33", "msg11", "msg1" ]
}, "t0" ],
[ "Email/get", {
    "accountId": "1",
    "state": "123456",
    "list": [{
        "id": "msg1023",
        "threadId": "trd194",
    }, {
        "id": "msg223",
        "threadId": "trd114"
    },
    ...
    ],
    "notFound": null
}, "t1" ],
[ "Thread/get", {
    "accountId": "1",
    "state": "123456",
    "list": [{
        "id: "trd194",
        "emailIds": [ "msg1020", "msg1021", "msg1023" ]
    }, {
        "id: "trd114",
        "emailIds": [ "msg201", "msg223" ]
    },
    ...
    ],
    "notFound": null
}, "t2" ]]
</artwork></figure>
<t>So to execute the final Email/get call, we look through the arguments and find there is one with a <spanx style="verb">#</spanx> prefix. To resolve this, we apply the algorithm:
</t>
<t>
<list style="numbers">
<t>Find the first response with client id &quot;t2&quot;. The &quot;Thread/get&quot; response
fulfils this criterion.</t>
<t>&quot;Thread/get&quot; is the name specified in the result reference, so this is fine.</t>
<t>Apply the <spanx style="emph">path</spanx> as a JSON pointer to the arguments object. Token-by-token:
a) <spanx style="verb">list</spanx>: get the array of thread objects
 b) <spanx style="verb">*</spanx>: for each of the items in the array:

<figure align="center"><artwork align="center">
i) `emailIds`: get the array of email ids
ii) Concatenate these into a single array of all the ids in the result.
</artwork></figure></t>
</list>
</t>
<t>The JMAP server now continues to process the Email/get call as though the arguments were:
</t>

<figure align="center"><artwork align="center">
{
    "ids": [ "msg1020", "msg1021", "msg1023", "msg201", "msg223", etc... ],
    "properties": [ "from", "receivedAt", "subject" ]
}
</artwork></figure>
</section>

<section anchor="security" title="Security">
<t>As always, the server must be strict about data received from the client. Arguments need to be checked for validity; a malicious user could attempt to find an exploit through the API. In case of invalid arguments (unknown/insufficient/wrong type for data etc.) the method MUST return an <spanx style="verb">invalidArguments</spanx> error and terminate.
</t>
</section>

<section anchor="concurrency" title="Concurrency">
<t>Each individual method call within a request MUST be serializable; concurrent execution of methods MUST produce the same effect as running them one at a time in some order.
</t>
<t>This means that the observable ordering may interleave method calls from different concurrent API requests, such that the data on the server may change between two method calls within a single API request.
</t>
</section>
</section>

<section anchor="standard-methods-and-naming-convention" title="Standard methods and naming convention">
<t>JMAP provides a uniform interface for creating, retrieving, updating and deleting objects of a particular type. For a <spanx style="verb">Foo</spanx> data type, records of that type would be fetched via a Foo/get call and modified via a Foo/set call. Delta updates may be fetched via a Foo/changes call. These methods all follow a standard format as described below.
</t>

<section anchor="get" title="/get">
<t>Objects of type <spanx style="strong">Foo</spanx> are fetched via a call to <spanx style="emph">Foo/get</spanx>.
</t>
<t>It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the Account to use. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">ids</spanx>: <spanx style="verb">String[]|null</spanx>
The ids of the Foo objects to return. If <spanx style="verb">null</spanx> then <spanx style="strong">all</spanx> records of the data type are returned, if this is supported for that data type.</t>
<t><spanx style="strong">properties</spanx>: <spanx style="verb">String[]|null</spanx>
If supplied, only the properties listed in the array are returned for each Foo object. If <spanx style="verb">null</spanx>, all properties of the object are returned. The id of the object is <spanx style="strong">always</spanx> returned, even if not explicitly requested. If an invalid property is requested, the call MUST be rejected with an <spanx style="verb">invalidArguments</spanx> error.</t>
</list>
</t>
<t>The response has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string representing the state on the server for <spanx style="strong">all</spanx> the data of this type in the account (not just the objects returned in this call). If the data changes, this string MUST change. If the Foo data is unchanged, servers SHOULD return the same state string on subsequent requests for this data type.
When a client receives a response with a different state string to a previous call, it MUST either throw away all currently cached objects for the type, or call <spanx style="emph">Foo/changes</spanx> to get the exact changes.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">Foo[]</spanx>
An array of the Foo objects requested. This is the <spanx style="strong">empty array</spanx> if no objects were found, or if the <spanx style="emph">ids</spanx> argument passed in was also the empty array. The results MAY be in a different order to the <spanx style="emph">ids</spanx> in the request arguments. If an identical id is included more than once in the request, the server MUST only include it once in either the <spanx style="emph">list</spanx> or <spanx style="emph">notFound</spanx> argument of the response.</t>
<t><spanx style="strong">notFound</spanx>: <spanx style="verb">String[]|null</spanx>
This array contains the ids passed to the method for records that do not exist. This property is <spanx style="verb">null</spanx> if all requested ids were found, or if the <spanx style="emph">ids</spanx> argument passed in was either <spanx style="verb">null</spanx> or the empty array.</t>
</list>
</t>
<t>The following additional error may be returned instead of the <spanx style="emph">Foo/get</spanx> response:
</t>
<t><spanx style="verb">requestTooLarge</spanx>: The number of <spanx style="emph">ids</spanx> requested by the client exceeds the maximum number the server is willing to process in a single method call.
</t>
</section>

<section anchor="changes" title="/changes">
<t>When the state of the set of Foo records changes on the server (whether due to creation, updates or deletion), the <spanx style="emph">state</spanx> property of the <spanx style="emph">Foo/get</spanx> response will change. The <spanx style="emph">Foo/changes</spanx> method allows a client to efficiently update the state of its Foo cache to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the Account to use. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">Foo/get</spanx> response. The server will return the changes that have occurred since this state.</t>
<t><spanx style="strong">maxChanges</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of ids to return in the response. The server MAY choose to return fewer than this value, but MUST NOT return more. If not given by the client, the server may choose how many to return. If supplied by the client, the value MUST be a positive integer greater than 0. If a value outside of this range is given, the server MUST reject the call with an <spanx style="verb">invalidArguments</spanx> error.</t>
</list>
</t>
<t>The response has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="emph">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">hasMoreChanges</spanx>: <spanx style="verb">Boolean</spanx>
If <spanx style="verb">true</spanx>, the client may call <spanx style="emph">Foo/changes</spanx> again with the <spanx style="emph">newState</spanx> returned to get further updates. If <spanx style="verb">false</spanx>, <spanx style="emph">newState</spanx> is the current server state.</t>
<t><spanx style="strong">changed</spanx>: <spanx style="verb">String[]|null</spanx>
An array of ids for records which have been created or modified but not destroyed since the oldState, or <spanx style="verb">null</spanx> if none.</t>
<t><spanx style="strong">destroyed</spanx>: <spanx style="verb">String[]|null</spanx>
An array of ids for records which have been destroyed since the old state,
or <spanx style="verb">null</spanx> if none.</t>
</list>
</t>
<t>If a <spanx style="emph">maxChanges</spanx> is supplied, or set automatically by the server, the server MUST ensure the number of ids returned across <spanx style="emph">changed</spanx> and <spanx style="emph">destroyed</spanx> does not exceed this limit. If there are more changes than this between the client's state and the current server state, the update returned SHOULD generate an update to take the client to an intermediate state, from which the client can continue to call <spanx style="emph">Foo/changes</spanx> until it is fully up to date. If it is unable to calculate an intermediate state, it MUST return a <spanx style="verb">cannotCalculateChanges</spanx> error response instead.
</t>
<t>If a Foo record has been modified AND destroyed since the oldState, the server SHOULD just return the id in the <spanx style="emph">destroyed</spanx> list, but MAY return it in the <spanx style="emph">changed</spanx> list as well. If a Foo record has been created AND destroyed since the oldState, the server SHOULD remove the id from the response entirely, but MAY include it in the <spanx style="emph">destroyed</spanx> list.
</t>
<t>The following additional errors may be returned instead of the <spanx style="emph">Foo/changes</spanx> response:
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: The server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old, or the server being unable to produce an update to an intermediate state when there are too many updates. The client MUST invalidate its Foo cache.
</t>
<t>Maintaining state to allow calculation of <spanx style="emph">Foo/changes</spanx> can be expensive for the server, but always returning <spanx style="emph">cannotCalculateChanges</spanx> severely increases network traffic and resource usage for the client. To allow efficient sync, servers SHOULD be able to calculate changes from any state string that was given to a client within the last 30 days (but of course may support calculating updates from states older than this).
</t>
</section>

<section anchor="set" title="/set">
<t>Modifying the state of Foo objects on the server is done via the <spanx style="emph">Foo/set</spanx> method. This encompasses creating, updating and destroying Foo records. This allows the server to sort out ordering and dependencies that may exist if doing multiple operations at once (for example to ensure there is always a minimum number of a certain record type).
</t>
<t>The <spanx style="emph">Foo/set</spanx> method takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the Account to use. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">ifInState</spanx>: <spanx style="verb">String|null</spanx>
This is a state string as returned by the <spanx style="emph">Foo/get</spanx> method. If supplied, the string must match the current state, otherwise the method will be aborted and a <spanx style="verb">stateMismatch</spanx> error returned. If <spanx style="verb">null</spanx>, any changes will be applied to the current state.</t>
<t><spanx style="strong">create</spanx>: <spanx style="verb">String[Foo]|null</spanx>
A map of <spanx style="emph">creation id</spanx> (an arbitrary string set by the client) to Foo
objects, or <spanx style="verb">null</spanx> if no objects are to be created.
The Foo object type definition MAY define default values for properties. Any such property MAY be omitted by the client.
The client MUST omit any properties that may only be set by the server (for example, the <spanx style="emph">id</spanx> property on most object types).</t>
<t><spanx style="strong">update</spanx>: <spanx style="verb">String[PatchObject]|null</spanx>
A map of id to a Patch object to apply to the current Foo object with that id, or <spanx style="verb">null</spanx> if no objects are to be updated.
A <spanx style="emph">PatchObject</spanx> is of type <spanx style="verb">String[*]</spanx>, and represents an unordered set of patches.  The keys are a path in <xref target="RFC6901"/> JSON pointer format, with an implicit leading &quot;/&quot; (i.e. prefix each key with &quot;/&quot; before applying the JSON pointer evaluation algorithm).
All paths MUST also conform to the following restrictions; if there is any violation, the update MUST be rejected with an <spanx style="verb">invalidPatch</spanx> error:
<list style="symbols">
<t>The pointer MUST NOT reference inside an array (i.e. you MUST NOT insert/delete from an array; the array MUST be replaced in its entirety instead).</t>
<t>All parts prior to the last (i.e. the value after the final slash) MUST already exist on the object being patched.</t>
<t>There MUST NOT be two patches in the PatchObject where the pointer of one is the prefix of the pointer of the other, e.g. &quot;alerts/1/offset&quot; and &quot;alerts&quot;.</t>
</list>
The value associated with each pointer determines how to apply that patch:
<list style="symbols">
<t>If <spanx style="verb">null</spanx>, set to the default value if specified for this property, otherwise remove the property from the patched object. If the key is not present in the parent, this a no-op.</t>
<t>Anything else: The value to set for this property (this may be a replacement or addition to the object being patched).</t>
</list>
Any server-set properties MAY be included in the patch if their value is identical to the current server value (before applying the patches to the object). Otherwise, the update MUST be rejected with an <spanx style="emph">invalidProperties</spanx> SetError.
This patch definition is designed such that an entire Foo object is also a valid PatchObject. The client MAY choose to optimise network usage by just sending the diff, or MAY just send the whole object; the server processes it the same either way.</t>
<t><spanx style="strong">destroy</spanx>: <spanx style="verb">String[]|null</spanx>
A list of ids for Foo objects to permanently delete, or <spanx style="verb">null</spanx> if no objects are to be destroyed.</t>
</list>
</t>
<t>Each creation, modification or destruction of an object is considered an atomic unit. It is permissible for the server to commit changes to some objects but not others, however it is not permissible to only commit part of an update to a single record (e.g. update a <spanx style="emph">name</spanx> property but not a <spanx style="emph">count</spanx> property, if both are supplied in the update object).
</t>
<t>The final state MUST be valid after the Foo/set is finished, however the server may have to transition through invalid intermediate states (not exposed to the client) while processing the individual create/update/destroy requests. For example, suppose there is a &quot;name&quot; property that must be unique. A single method call could rename an object A =&gt; B, and simultaneously rename another object B =&gt; A. If the final state is valid, this is allowed. Otherwise, each creation, modification or destruction of an object should be processed sequentially and accepted/rejected based on the current server state.
</t>
<t>If a create, update or destroy is rejected, the appropriate error MUST be added to the notCreated/notUpdated/notDestroyed property of the response and the server MUST continue to the next create/update/destroy. It does not terminate the method.
</t>
<t>If an id given cannot be found, the update or destroy MUST be rejected with a <spanx style="verb">notFound</spanx> set error.
</t>
<t>The server MAY skip an update (rejecting it with a <spanx style="verb">willDestroy</spanx> SetError) if that object is destroyed in the same /set request.
</t>
<t>Some record objects may hold references to others (foreign keys). When records are created or modified, they may reference other records being created <spanx style="emph">in the same API request</spanx> by using the creation id prefixed with a <spanx style="verb">#</spanx>. The order of the method calls in the request by the client MUST be such that the record being referenced is created in the same or an earlier call. The server thus never has to look ahead. Instead, while processing a request (a series of method calls), the server MUST keep a simple map for the duration of the request of creation id to record id for each newly created record, so it can substitute in the correct value if necessary in later method calls.
</t>
<t>Creation ids are scoped by type; a separate <spanx style="verb">creation id -&gt; id</spanx> map MUST be kept for each type for the duration of the request. Foreign key references are always for a particular record type, so use of the same creation key in two different types cannot cause any ambiguity. Creation ids sent by the client SHOULD be unique within the single API request for a particular data type. If a creation id is reused for the same type, the server MUST map the creation id to the most recently created item with that id.
</t>
<t>The response has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String|null</spanx>
The state string that would have been returned by <spanx style="emph">Foo/get</spanx> before making the requested changes, or <spanx style="verb">null</spanx> if the server doesn't know what the previous state string was.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
The state string that will now be returned by <spanx style="emph">Foo/get</spanx>.</t>
<t><spanx style="strong">created</spanx>: <spanx style="verb">String[Foo]|null</spanx>
A map of the creation id to an object containing any properties of the created Foo object that were not sent by the client. This includes all server-set properties (such as the <spanx style="emph">id</spanx> in most object types) and any properties that were omitted by the client and so set to a default by the server.
This argument is <spanx style="verb">null</spanx> if no Foo objects were successfully created.</t>
<t><spanx style="strong">updated</spanx>: <spanx style="verb">String[Foo|null]|null</spanx>
The <spanx style="emph">keys</spanx> in this map are the ids of all Foos that were successfully updated, or <spanx style="verb">null</spanx> if none successful.
The <spanx style="emph">value</spanx> for each id is a Foo object containing any property that
changed in a way <spanx style="emph">not</spanx> explicitly requested by the <spanx style="emph">PatchObject</spanx> sent to the server, or <spanx style="verb">null</spanx> if none. This lets the client know of any changes to server-set or computed properties.</t>
<t><spanx style="strong">destroyed</spanx>: <spanx style="verb">String[]|null</spanx>
A list of Foo ids for records that were successfully destroyed, or <spanx style="verb">null</spanx> if none successful.</t>
<t><spanx style="strong">notCreated</spanx>: <spanx style="verb">String[SetError]|null</spanx>
A map of creation id to a SetError object for each record that failed to be created, or <spanx style="verb">null</spanx> if all successful.</t>
<t><spanx style="strong">notUpdated</spanx>: <spanx style="verb">String[SetError]|null</spanx>
A map of Foo id to a SetError object for each record that failed to be updated, or <spanx style="verb">null</spanx> if all successful.</t>
<t><spanx style="strong">notDestroyed</spanx>: <spanx style="verb">String[SetError]|null</spanx>
A map of Foo id to a SetError object for each record that failed to be destroyed, or <spanx style="verb">null</spanx> if all successful.</t>
</list>
</t>
<t>A <spanx style="strong">SetError</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">type</spanx>: <spanx style="verb">String</spanx>
The type of error.</t>
<t><spanx style="strong">description</spanx>: <spanx style="verb">String|null</spanx>
A description of the error to display to the user.</t>
</list>
</t>
<t>The following SetError types are defined and may be returned for set operations on any record type where appropriate:
</t>
<t>
<list style="symbols">
<t><spanx style="verb">forbidden</spanx>: (create; update; destroy) The create/update/destroy would
violate an ACL or other permissions policy.</t>
<t><spanx style="verb">overQuota</spanx>: (create) The create would exceed a server-defined limit on
the number or total size of objects of this type.</t>
<t><spanx style="verb">rateLimit</spanx>: (create) Too many objects of this type have been created
recently, and a server-defined rate limit has been reached. It may work if
tried again later.</t>
<t><spanx style="verb">notFound</spanx>: (update; destroy) The id given cannot be found.</t>
<t><spanx style="verb">invalidPatch</spanx>: (update) The PatchObject given to update the record was
not a valid patch (see the patch description).</t>
<t><spanx style="verb">willDestroy</spanx> (update)
The client requested an object be both updated and destroyed in the same
/set request, and the server has decided to therefore ignore the update.</t>
<t><spanx style="verb">invalidProperties</spanx>: (create; update) The record given is invalid in
some way. For example:
<list style="symbols">
<t>It contains properties which are invalid according to the type specification of this record type.</t>
<t>It contains a property that may only be set by the server (e.g. &quot;id&quot;) and are different to the current value. Note, to allow clients to pass whole objects back, it is not an error to include a server-set property so long as the value is identical to the current value on the server (or the value that will be set by the server if a create).</t>
<t>There is a reference to another record (foreign key) and the given id does not correspond to a valid record.</t>
</list>
The SetError object SHOULD also have a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid.
Individual methods MAY specify more specific errors for certain conditions that would otherwise result in an invalidProperties error. If the condition of one of these is met, it MUST be returned instead of the invalidProperties error.</t>
<t><spanx style="verb">singleton</spanx>: (create; destroy) This is a singleton type, so you cannot create
another one or destroy the existing one.</t>
</list>
</t>
<t>Other possible SetError types MAY be given in specific method descriptions. Other properties MAY also be present on the <spanx style="emph">SetError</spanx> object, as described in the relevant methods.
</t>
<t>The following additional errors may be returned instead of the <spanx style="emph">Foo/set</spanx> response:
</t>
<t><spanx style="verb">requestTooLarge</spanx>: The total number of objects to create, update or destroy exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">stateMismatch</spanx>: An <spanx style="verb">ifInState</spanx> argument was supplied and it does not match the current state.
</t>
</section>

<section anchor="query" title="/query">
<t>For data sets where the total amount of data is expected to be very small, clients can just fetch the complete set of data and then do any sorting/filtering locally. However, for large data sets (e.g. multi-gigabyte mailboxes), the client needs to be able to search/sort/window the data type on the server.
</t>
<t>A query on the set of Foos in an account is made by calling <spanx style="emph">Foo/query</spanx>. This takes a number of arguments to determine which records to include, how they should be sorted, and which part of the result should be returned (the full list may be <spanx style="emph">very</spanx> long). The result is returned as a list of Foo ids.
</t>
<t>A call to <spanx style="emph">Foo/query</spanx> takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the Account to use. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterOperator|FilterCondition|null</spanx>
Determines the set of Foos returned in the results. If <spanx style="verb">null</spanx>, all objects in the account of this type are included in the results.
A <spanx style="strong">FilterOperator</spanx> object has the following properties:
<list style="symbols">
<t><spanx style="strong">operator</spanx>: <spanx style="verb">String</spanx>
This MUST be one of the following strings: &quot;AND&quot;/&quot;OR&quot;/&quot;NOT&quot;:
<list style="symbols">
<t><spanx style="strong">AND</spanx>: all of the conditions must match for the filter to match.</t>
<t><spanx style="strong">OR</spanx>: at least one of the conditions must match for the filter to match.</t>
<t><spanx style="strong">NOT</spanx>: none of the conditions must match for the filter to match.</t>
</list></t>
<t><spanx style="strong">conditions</spanx>: <spanx style="verb">(FilterOperator|FilterCondition)[]</spanx>
The conditions to evaluate against each email.</t>
</list>
A <spanx style="strong">FilterCondition</spanx> is an <spanx style="verb">object</spanx>, whose allowed properties and semantics depend on the data type and is defined in the <spanx style="emph">/query</spanx> method specification for that type.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">Comparator[]|null</spanx>
Lists the names of properties to compare between two Foo records, and how to compare them, to determine which comes first in the sort. If two Foo records have an identical value for the first comparator, the next comparator will be considered and so on. If all comparators are the same (this includes the case where an empty array or <spanx style="verb">null</spanx> is given as the <spanx style="emph">sort</spanx> argument), the sort order is server-dependent, but MUST be stable between calls to Foo/query.
A <spanx style="strong">Comparator</spanx> has the following properties:
<list style="symbols">
<t><spanx style="strong">property</spanx>: <spanx style="verb">String</spanx>
The name of the property on the Foo objects to compare.</t>
<t><spanx style="strong">isAscending</spanx>: <spanx style="verb">Boolean</spanx> (optional; default: <spanx style="verb">true</spanx>)
If true, sort in ascending order. If false, reverse the comparator's results to sort in descending order.</t>
<t><spanx style="strong">collation</spanx>: <spanx style="verb">String</spanx> (optional; default is server-dependent)
The identifier, as registered in the collation registry defined in <xref target="RFC4790"/>, for the algorithm to use when comparing the order of strings. The algorithms the server supports are advertised in the capabilities object returned with the JMAP Session object.
If omitted, the default algorithm is server-dependent, but:
<list style="numbers">
<t>It MUST be unicode-aware.</t>
<t>It SHOULD have reasonable default behavior for many languages when
the user's language is unknown.</t>
<t>It MAY be selected based on out-of-band information about the user's
language/locale.</t>
<t>It SHOULD be case-insensitive where such a concept makes sense for a
language/locale.</t>
</list>
The &quot;i;unicode-casemap&quot; collation (<xref target="RFC5051"/>) and the Unicode Collation Algorithm (<eref target="http://www.unicode.org/reports/tr10/"/>) are two examples that fulfil these criterion.
When the property being compared is not a string, the <spanx style="emph">collation</spanx> property is ignored and the following comparison rules apply based on the type. In ascending order:
<list style="symbols">
<t><spanx style="verb">Boolean</spanx>: <spanx style="verb">false</spanx> comes before <spanx style="verb">true</spanx>.</t>
<t><spanx style="verb">Number</spanx>: A lower number comes before a higher number.</t>
<t><spanx style="verb">Date</spanx>/<spanx style="verb">UTCDate</spanx>: The earlier date comes first.</t>
</list></t>
</list></t>
<t><spanx style="strong">position</spanx>: <spanx style="verb">Number</spanx> (default: <spanx style="verb">0</spanx>)
The 0-based index of the first id in the full list of results to return.
If a negative value is given, it is an offset from the end of the list. Specifically, the negative value MUST be added to the total number of results given the filter, and if still negative clamped to <spanx style="verb">0</spanx>. This is now the 0-based index of the first id to return.
If the index is greater than or equal to the total number of objects in the results list then the <spanx style="emph">ids</spanx> array in the response will be empty, but this is not an error.</t>
<t><spanx style="strong">anchor</spanx>: <spanx style="verb">String|null</spanx>
A Foo id. If supplied the <spanx style="emph">position</spanx> argument is ignored. The index of this id in the results will be used in combination with the <spanx style="verb">anchorOffset</spanx> argument to determine the index of the first result to return (see below for more details).</t>
<t><spanx style="strong">anchorOffset</spanx>: <spanx style="verb">Number|null</spanx>
The index of the anchor object relative to the index of the first result to return. This MAY be negative. For example, <spanx style="verb">-1</spanx> means the first Foo after the anchor Foo should be the first result in the results returned (see below for more details).</t>
<t><spanx style="strong">limit</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of results to return. If <spanx style="verb">null</spanx>, no limit presumed. The server MAY choose to enforce a maximum <spanx style="verb">limit</spanx> argument. In this case, if a greater value is given (or if it is <spanx style="verb">null</spanx>), the limit should be clamped to the maximum; since the total number of results in the search is returned, the client can determine if it has received all the results. If a negative value is given, the call MUST be rejected with an <spanx style="verb">invalidArguments</spanx> error.</t>
</list>
</t>
<t>If an <spanx style="strong">anchor</spanx> argument is given, then after filtering and sorting the anchor is looked for in the results. If found, the <spanx style="strong">anchor offset</spanx> is then subtracted from its index. If the resulting index is now negative, it is clamped to 0. This index is now used exactly as though it were supplied as the <spanx style="verb">position</spanx> argument. If the anchor is not found, the call is rejected with an <spanx style="verb">anchorNotFound</spanx> error.
</t>
<t>If an <spanx style="emph">anchor</spanx> is specified, any position argument supplied by the client MUST be ignored. If <spanx style="emph">anchorOffset</spanx> is <spanx style="verb">null</spanx>, it defaults to <spanx style="verb">0</spanx>. If no <spanx style="emph">anchor</spanx> is supplied, any anchor offset argument MUST be ignored.
</t>
<t>A client can use <spanx style="emph">anchor</spanx> instead of <spanx style="emph">position</spanx> to find the index of an id
within a large set of results.
</t>
<t>The response has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterOperator|FilterCondition|null</spanx>
The filter to apply to the search. Echoed back from the call.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">Comparator[]|null</spanx>
The sort options used. Echoed back from the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string encoding the current state on the server. This string MUST change if the results of the search <spanx style="emph">may</spanx> have changed (for example, there has been a change to the state of the set of Foos; it does not <spanx style="emph">guarantee</spanx> that anything in the search has changed). It may be passed to <spanx style="emph">Foo/queryChanges</spanx> to efficiently get the set of changes from the client's current state.
Should a client receive back a response with a different state string to a previous call, it MUST either throw away the currently cached search and fetch it again (note, this does not require fetching the records again, just the list of ids) or, call <spanx style="emph">Foo/queryChanges</spanx> to get the delta difference.</t>
<t><spanx style="strong">canCalculateChanges</spanx>: <spanx style="verb">Boolean</spanx>
This is <spanx style="verb">true</spanx> if the server supports calling <spanx style="emph">Foo/queryChanges</spanx> with these <spanx style="verb">filter</spanx>/<spanx style="verb">sort</spanx> parameters. Note, this does not guarantee that the <spanx style="emph">Foo/queryChanges</spanx> call will succeed, as it may only be possible for a limited time afterwards due to server internal implementation details.</t>
<t><spanx style="strong">position</spanx>: <spanx style="verb">Number</spanx>
The 0-based index of the first result in the <spanx style="verb">ids</spanx> array within the complete list of search results.</t>
<t><spanx style="strong">total</spanx>: <spanx style="verb">Number</spanx>
The total number of foos in the results (given the <spanx style="emph">filter</spanx>).</t>
<t><spanx style="strong">ids</spanx>: <spanx style="verb">String[]</spanx>
The list of ids for each foo in the search results, starting at the index given by the <spanx style="emph">position</spanx> argument of this response, and continuing until it hits the end of the results or reaches the <spanx style="verb">limit</spanx> number of ids. If <spanx style="emph">position</spanx> is &gt;= <spanx style="emph">total</spanx>, this MUST be the empty list.</t>
</list>
</t>
<t>The following additional errors may be returned instead of the <spanx style="emph">Foo/query</spanx> response:
</t>
<t><spanx style="verb">anchorNotFound</spanx>: An anchor argument was supplied, but it cannot be found in the results of the search.
</t>
<t><spanx style="verb">unsupportedSort</spanx>: The <spanx style="emph">sort</spanx> is syntactically valid, but includes a property the server does not support sorting on, or a collation method it does not recognise.
</t>
<t><spanx style="verb">unsupportedFilter</spanx>: The <spanx style="emph">filter</spanx> is syntactically valid, but the server cannot process it.
</t>
</section>

<section anchor="querychanges" title="/queryChanges">
<t>The <spanx style="verb">Foo/queryChanges</spanx> call allows a client to efficiently update the state of any cached foo search to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, the primary account will be used.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterOperator|FilterCondition|null</spanx>
The filter argument that was used with <spanx style="emph">Foo/query</spanx>.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">Comparator[]|null</spanx>
The sort argument that was used with <spanx style="emph">Foo/query</spanx>.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">Foo/query</spanx> response. The server will return the changes made since this state.</t>
<t><spanx style="strong">maxChanges</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of changes to return in the response. See error descriptions below for more details.</t>
<t><spanx style="strong">uptoId</spanx>: <spanx style="verb">String|null</spanx>
The last (highest-index) id the client currently has cached from the search results. When there are a large number of results, in a common case the client may have only downloaded and cached a small subset from the beginning of the results. If the sort and filter are both only on immutable properties, this allows the server to omit changes after this point in the results, which can significantly increase efficiency. If they are not immutable, this argument is ignored.</t>
</list>
</t>
<t>The response has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterOperator|FilterCondition|null</spanx>
The filter to apply to the search. Echoed back from the call.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">Comparator[]|null</spanx>
The sort options used. Echoed back from the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="verb">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">uptoId</spanx>: <spanx style="verb">String|null</spanx>
Echoed back from the call.</t>
<t><spanx style="strong">total</spanx>: <spanx style="verb">Number</spanx>
The total number of foos in the results (given the <spanx style="emph">filter</spanx>).</t>
<t><spanx style="strong">removed</spanx>: <spanx style="verb">String[]</spanx>
The <spanx style="emph">id</spanx> for every foo that was in the results in the old state and is not in the results in the new state.
If the sort and filter are both only on immutable properties and an <spanx style="emph">uptoId</spanx> is supplied and exists in the results, any ids that were removed but have a higher index than <spanx style="emph">uptoId</spanx> SHOULD be omitted.
If the server cannot calculate this exactly, the server MAY return extra foos in addition that may have been in the old results but are not in the new results.
If the <spanx style="emph">filter</spanx> or <spanx style="emph">sort</spanx> includes a mutable property, the server MUST include all foos in the current results for which this property MAY have changed.</t>
<t><spanx style="strong">added</spanx>: <spanx style="verb">AddedItem[]</spanx>
The id and index in the search results (in the new state) for every foo that has been added to the results since the old state AND every foo in the current results that was included in the <spanx style="emph">removed</spanx> array (due to a filter or sort based upon a mutable property).
If the sort and filter are both only on immutable properties and an <spanx style="emph">uptoId</spanx> is supplied and exists in the results, any ids that were added but have a higher index than <spanx style="emph">uptoId</spanx> SHOULD be omitted.
The array MUST be sorted in order of index, lowest index first.
An <spanx style="strong">AddedItem</spanx> object has the following properties:
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx></t>
<t><spanx style="strong">index</spanx>: <spanx style="verb">Number</spanx></t>
</list></t>
</list>
</t>
<t>The result of this is that if the client has a cached sparse array of foo ids in the results in the old state:
</t>

<figure align="center"><artwork align="center">
fooIds = [ "id1", "id2", null, null, "id3", "id4", null, null, null ]
</artwork></figure>
<t>then if it <spanx style="strong">splices out</spanx> all foos in the removed array:
</t>

<figure align="center"><artwork align="center">
removed = [ "id2", … ];
fooIds =&gt; [ "id1", null, null, "id3", "id4", null, null, null ]
</artwork></figure>
<t>and <spanx style="strong">splices in</spanx> (in order) all of the foos in the added array:
</t>

<figure align="center"><artwork align="center">
added = [{ id: "id5", index: 0, … }];
fooIds =&gt; [ "id5", "id1", null, null, "id3", "id4", null, null, null ]
</artwork></figure>
<t>and <spanx style="strong">truncates</spanx> or <spanx style="strong">extends</spanx> to the new total length, then the results will now be in the new state.
</t>
<t>The following additional errors may be returned instead of the <spanx style="emph">Foo/queryChanges</spanx> response:
</t>
<t><spanx style="verb">tooManyChanges</spanx>: There are more changes the the client's <spanx style="emph">maxChanges</spanx> argument. Each item in the removed or added array is considered as one change. The client may retry with a higher max changes or invalidate its cache of the search results.
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: The server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old. The client MUST invalidate its cache of the search results.
</t>
</section>

<section anchor="examples" title="Examples">
<t>Suppose we have a type <spanx style="emph">Todo</spanx> with the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx> (immutable; server-set)
The id of the object.</t>
<t><spanx style="strong">title</spanx>: <spanx style="verb">String</spanx>
A brief summary of what is to be done.</t>
<t><spanx style="strong">keywords</spanx>: <spanx style="verb">String[Boolean]</spanx> (mutable; default: <spanx style="verb">{}</spanx>)
A set of keywords that apply to the todo. The set is represented as an object, with the keys being the <spanx style="emph">keywords</spanx>. The value for each key in the object MUST be <spanx style="verb">true</spanx>.</t>
<t><spanx style="strong">neuralNetworkTimeEstimation</spanx>: <spanx style="verb">Number</spanx> (server-set)
The title and keywords are fed into the server's state-of-the-art neural
network to get an estimation of how long this todo will take, in seconds.</t>
</list>
</t>
<t>and the server supports querying by keyword using the syntax <spanx style="verb">{ hasKeyword: "foo" }</spanx> in the <spanx style="emph">filter</spanx> argument to <spanx style="emph">/query</spanx>.
</t>
<t>Now, a client might want to display the list of todos with a particular query, so it makes the following method call:
</t>

<figure align="center"><artwork align="center">
[["Todo/query", {
  "filter": { "hasKeyword": "music" },
  "sort": [{ "property": "title" }],
  "position": 0,
  "limit": 10
}, "0"],
["Todo/get", {
  "#ids": {
    "resultOf": "0",
    "name": "Todo/query",
    "path": "/ids"
  },
}, "1"]]
</artwork></figure>
<t>This would query the server for the set of todos with a keyword of &quot;music&quot;, sorted by title, and limited to the first 10 results. It fetches the full object for each of these Todos using backreferences to reference the result of the query. The response might look something like:
</t>

<figure align="center"><artwork align="center">
[["Todo/query", {
  "accountId": "x",
  "filter": { "hasKeyword": "music" },
  "sort": [{ "property": "title" }],
  "state": "y13213",
  "canCalculateChanges": true,
  "position": 0,
  "total": 26,
  "ids": [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" ]
}, "0"],
["Todo/get", {
  "accountId": "x",
  "state": "10324",
  "list": [{
    "id": "a",
    "title": "Practise Piano",
    "keywords": {
      "music": true,
      "beethoven": true,
      "mozart": true,
      "liszt": true,
      "rachmaninov": true
    },
    "neuralNetworkTimeEstimation": 3600
  }, {
    "id": "b",
    "title": "Listen to Daft Punk",
    "keywords": {
      "music": true,
      "trance": true
    },
    "neuralNetworkTimeEstimation": 18000
  },
  ...
  ]
}, "1"]]
</artwork></figure>
<t>Now suppose the user adds a keyword &quot;chopin&quot; and removes the keyword &quot;mozart&quot; from the &quot;Practise Piano&quot; task. The client may send the whole object to the server, as this is a valid PatchObject:
</t>

<figure align="center"><artwork align="center">
[["Todo/set", {
  "ifInState": "10324",
  "update": {
    "a": {
      "id": "a",
      "title": "Practise Piano",
      "keywords": {
        "music": true,
        "beethoven": true,
        "chopin": true,
        "liszt": true,
        "rachmaninov": true,
      }
      "neuralNetworkTimeEstimation": 360
    }
  }
}, "0"]]
</artwork></figure>
<t>or it may send a minimal patch:
</t>

<figure align="center"><artwork align="center">
[["Todo/set", {
  "ifInState": "10324",
  "update": {
    "a": {
      "keywords/chopin": true,
      "keywords/mozart": null
    }
  }
}, "0"]]
</artwork></figure>
<t>The effect is exactly the same on the server in either case, and presuming the server is still in state &quot;10324&quot; it will probably return success:
</t>

<figure align="center"><artwork align="center">
[["Todo/set", {
  "accountId": "x",
  "oldState": "10324",
  "newState": "10329",
  "updated": {
    "a": {
      "neuralNetworkTimeEstimation": 5400
    }
  }
}, "0"]]
</artwork></figure>
<t>The server changed the &quot;neuralNetworkTimeEstimation&quot; property on the object as part of this change; as this changed in a way <spanx style="emph">not</spanx> explicitly requested by the PatchObject sent to the server, it is returned with the &quot;updated&quot; confirmation.
</t>
<t>Now, suppose another user deleted the &quot;Listen to Daft Punk&quot; todo. The first user will receive a push notification (see later in the spec) with the changed state string for the &quot;Todo&quot; type. Since the new string does not match its current state, it knows it needs to check for updates. It may make a request like:
</t>

<figure align="center"><artwork align="center">
[["Todo/changes", {
  "accountId": "x",
  "sinceState": "10324",
  "maxChanges": 50,
}, "0"],
["Todo/queryChanges", {
  "filter": { "hasKeyword": "music" },
  "sort": [{ "property": "title" }],
  "sinceState": "y13213"
  "maxChanges": 50,
}, "1"]]
</artwork></figure>
<t>and receive in response:
</t>

<figure align="center"><artwork align="center">
[["Todo/changes", {
  "accountId": "x",
  "oldState": "10324",
  "newState": "871903",
  "hasMoreChanges": false,
  "changed": null,
  "destroyed": ["b"]
}, "0"],
["Todo/queryChanges", {
  "filter": { "hasKeyword": "music" },
  "sort": [{ "property": "title" }],
  "oldState": "y13213"
  "newState": "y13218"
  "total": 25,
  "removed": ["b"],
  "added": null
}, "1"]]
</artwork></figure>
</section>
</section>

<section anchor="binary-data" title="Binary data">
<t>Binary data is referenced by a <spanx style="emph">blobId</spanx> in JMAP, and uploaded/downloaded separately to the core API. A blobId does not have a name inherent to it, but this is normally given in the same object that contains the blobId. The data represented by a blobId is immutable.
</t>
<t>Any blobId that exists within an account may be used when creating/updating another object in that account. For example, an Email type may have a blobId that represents the RFC5322 representation of the message. A client could create a new Email object with an attachment and use this blobId, in effect attaching the old message to the new one. Similarly it could attach any existing existing attachment of an old message without having to download and upload it again.
</t>
<t>When the client uses a blobId in a create/update, the server MAY assign a new blobId to refer to the same binary data from the new/updated object. If it does so, it MUST return any properties that contain a changed blobId in the created/updated response so the client gets the new ids.
</t>
<t>A blob that is not referenced by a JMAP object (e.g. as a message attachment), MAY be deleted by the server to free up resources. Uploads (see below) are initially unreferenced blobs. To ensure interoperability:
</t>
<t>
<list style="symbols">
<t>The server SHOULD use a separate quota for unreferenced blobs to the user's
usual quota.</t>
<t>This quota SHOULD be at least the maximum total size that a single
object can reference on this server. For example, if supporting JMAP Mail, this should be at least the maximum total attachments size for a message.</t>
<t>When an upload would take the user over quota, the server MUST delete
unreferenced blobs in date order, oldest first, until there is room for the new blob.</t>
<t>Except where quota restrictions force early deletion, an unreferenced blob
SHOULD NOT be deleted for at least 24h from the time of upload; if reuploaded, the same blobId MAY be returned, but this SHOULD reset the expiry time.</t>
<t>A blob MUST NOT be deleted during the method call which removed the last
reference, so that a client can issue a create and a destroy that both reference the blob within the same method call.</t>
</list>
</t>

<section anchor="uploading-binary-data" title="Uploading binary data">
<t>There is a single endpoint which handles all file uploads for an account, regardless of what they are to be used for. The JMAP Session object has an <spanx style="emph">uploadUrl</spanx> property in <xref target="RFC6570"/> URI Template (level 1) format, which MAY contain a variable called <spanx style="verb">accountId</spanx>. The client may use this template in combination with an <spanx style="emph">accountId</spanx> (if required in the template) to get the URL of the file upload resource.
</t>
<t>To upload a file, the client submits an authenticated POST request to the file upload resource.
</t>
<t>A successful request MUST return a single JSON object with the following properties as the response:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">blobId</spanx>: <spanx style="verb">String</spanx>,
The id representing the binary data uploaded. The data for this id is immutable. The id <spanx style="emph">only</spanx> refers to the binary data, not any metadata.</t>
<t><spanx style="strong">type</spanx>: <spanx style="verb">String</spanx>
The media type of the file (as specified in <xref target="RFC6838"/>, section 4.2) as set in the Content-Type header of the upload HTTP request, with CFWS collapsed to SP and <xref target="RFC2231"/>/<xref target="RFC2047"/> encoding removed.</t>
<t><spanx style="strong">size</spanx>: <spanx style="verb">Number</spanx>
The size of the file in octets.</t>
</list>
</t>
<t>If identical binary content to an existing blob in the account is uploaded, the existing blobId MAY be returned.
</t>
</section>

<section anchor="downloading-binary-data" title="Downloading binary data">
<t>The JMAP Session object has a <spanx style="emph">downloadUrl</spanx> property, which is in <xref target="RFC6570"/> URI Template (level 1) format. The URL MUST contain a variable called <spanx style="verb">blobId</spanx>, MAY contain a variable called <spanx style="verb">accountId</spanx>, and SHOULD contain a variable called <spanx style="verb">name</spanx>.
</t>
<t>The client may use this template in combination with an <spanx style="emph">accountId</spanx> (if required in the URL template) and <spanx style="emph">blobId</spanx> to download any binary data (files) referenced by other objects. Since a blob is not associated with a particular name, the template SHOULD allow a name to be substituted in as well; the server will return this as the filename if it sets a <spanx style="verb">Content-Disposition</spanx> header.
</t>
<t>To download the data the client makes an authenticated GET request to the download URL with the appropriate variables substituted in. The client SHOULD send an <spanx style="verb">Accept</spanx> header with the content type they would like the server to return for the file. The <spanx style="verb">Content-Type</spanx> header of a successful response SHOULD be set to the type as requested in the <spanx style="verb">Accept</spanx> header by the client, or <spanx style="verb">application/octet-stream</spanx> if unknown and no <spanx style="verb">Accept</spanx> header given.
</t>
</section>

<section anchor="blobcopy" title="Blob/copy">
<t>Binary data may be copied <spanx style="strong">between</spanx> two different accounts using the <spanx style="emph">Blob/copy</spanx> method, rather than having to download then reupload on the client.
</t>
<t>The <spanx style="emph">Blob/copy</spanx> method takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">fromAccountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to copy blobs from. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">toAccountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to copy blobs to. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">blobIds</spanx>: <spanx style="verb">String[]</spanx>
A list of ids of blobs to copy to the other account.</t>
</list>
</t>
<t>The response has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">fromAccountId</spanx>: <spanx style="verb">String</spanx>
The id of the account emails were copied from.</t>
<t><spanx style="strong">toAccountId</spanx>: <spanx style="verb">String</spanx>
The id of the account emails were copied to.</t>
<t><spanx style="strong">copied</spanx>: <spanx style="verb">String[String]|null</spanx>
A map of the blob id in the <spanx style="emph">fromAccount</spanx> to the id for the blob in the <spanx style="emph">toAccount</spanx>, or <spanx style="verb">null</spanx> if none were successfully copied.</t>
<t><spanx style="strong">notCopied</spanx>: <spanx style="verb">String[SetError]|null</spanx>
A map of blob id to a SetError object for each blob that failed to be copied, <spanx style="verb">null</spanx> if none.</t>
</list>
</t>
<t>The <spanx style="strong">SetError</spanx> may be any of the standard set errors that may be returned for a <spanx style="emph">create</spanx>.
</t>
<t>The following additional errors may be returned instead of the <spanx style="emph">Blob/copy</spanx> response:
</t>
<t><spanx style="verb">fromAccountNotFound</spanx>: A <spanx style="emph">fromAccountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">toAccountNotFound</spanx>: A <spanx style="emph">toAccountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
</section>
</section>

<section anchor="push" title="Push">
<t>Push notifications allow clients to efficiently update (almost) instantly to stay in sync with data changes on the server. In JMAP, push notifications occur out-of-band (i.e. not over the same connection as API exchanges), so that they can make use of efficient native push mechanisms on different platforms.
</t>
<t>The general model for push is simple and sends minimal data over the push channel. The format allows multiple changes to be coalesced into a single push update, and the frequency of pushes to be rate limited by the server. It doesn't matter if some push events are dropped before they reach the client; it will still get all changes next time it syncs.
</t>

<section anchor="the-statechange-object" title="The StateChange object">
<t>When something changes on the server, the server pushes a <spanx style="strong">StateChange</spanx> object to the client. A <spanx style="strong">StateChange</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">changed</spanx>: <spanx style="verb">String[TypeState]</spanx>
A map of <spanx style="emph">account id</spanx> to an object encoding the state of data types that have changed for that account since the last push event, for each of the accounts to which the user has access and for which something has changed.
A <spanx style="strong">TypeState</spanx> object is a map. The keys are the type name <spanx style="verb">Foo</spanx> (e.g. &quot;Mailbox&quot; or &quot;Email&quot;), and the value is the <spanx style="emph">state</spanx> property that would currently be returned by a call to <spanx style="emph">Foo/get</spanx>.
The client can compare the new state strings with its current values to see whether it has the current data for these types. If not, the changes can then be efficiently fetched in a single standard API request (using the <spanx style="emph">/changes</spanx> type methods).</t>
<t><spanx style="strong">trigger</spanx>: <spanx style="verb">String</spanx>
What caused this change. The following causes are defined:
<list style="symbols">
<t><spanx style="verb">delivery</spanx>: The arrival of a new message caused the change.</t>
<t><spanx style="verb">user</spanx>: An action by the user caused the change.</t>
<t><spanx style="verb">unknown</spanx>: The cause of the change is unknown.</t>
</list>
Future specifications may define further values. Clients MUST treat an
unrecognised value the same as <spanx style="verb">unknown</spanx>.
Clients in battery constrained environments may use this information to decide whether to immediately fetch the changes.</t>
</list>
</t>
</section>

<section anchor="pushsubscription" title="PushSubscription">
<t>A push subscription is a message delivery context established between the client and a push service. A <spanx style="strong">PushSubscription</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">url</spanx>: <spanx style="verb">String</spanx>
An absolute URL where the JMAP server will POST the data for the push message.
This MUST begin with <spanx style="verb">https://</spanx>.</t>
<t><spanx style="strong">expires</spanx>: <spanx style="verb">UTCDate|null</spanx>
The time this push subscription expires. If specified, the JMAP server MUST NOT make further requests to this resource after this time. It MAY automatically remove the push subscription at or after this time.</t>
<t><spanx style="strong">keys</spanx>: <spanx style="verb">Object|null</spanx>
Client-generated encryption keys. If supplied the server MUST use them as specified in <xref target="RFC8291"/> to encrypt all data sent to the push subscription. The object MUST have the following properties:
<list style="symbols">
<t><spanx style="strong">p256dh</spanx>: the P-256 ECDH Diffie-Hellman public key as described in <xref target="RFC8291"/>, encoded in URL-safe base64 representation as defined in <xref target="RFC4648"/>.</t>
<t><spanx style="strong">auth</spanx>: the authentication secret as described in <xref target="RFC8291"/>, encoded in URL-safe base64 representation as defined in <xref target="RFC4648"/>.</t>
</list></t>
</list>
</t>
<t>Clients may register the push subscription with the JMAP server, which will then make a POST request to the associated push endpoint whenever an event occurs.
</t>
<t>The POST request MUST have a content type of <spanx style="verb">application/json</spanx> and contain the utf-8 JSON encoded <spanx style="emph">StateChange</spanx> object as the body. The request MUST have a <spanx style="verb">TTL</spanx> header, and MAY have <spanx style="verb">Urgency</spanx> and/or <spanx style="verb">Topic</spanx> headers, as specified in section 5 of <xref target="RFC8030"/>.
</t>
<t>If the response code is <spanx style="verb">503</spanx> (Service Unavailable), the JMAP server MAY try again later, but may also just drop the event. If the response code is <spanx style="verb">429</spanx> (Too Many Requests) the JMAP server SHOULD attempt to reduce the frequency of pushes to that URL. Any other <spanx style="verb">4xx</spanx> or <spanx style="verb">5xx</spanx> response code MUST be considered a <spanx style="strong">permanent failure</spanx> and the push subscription should be deregistered (not tried again even for future events unless explicitly re-registered by the client).
</t>
<t>The use of this push endpoint conforms with the use of a push endpoint by an Application Server as defined in <xref target="RFC8030"/>. A client MAY use the rest of <xref target="RFC8030"/> in combination with its own Push Server to form a complete end-to-end solution, or MAY rely on alternative mechanisms to ensure the delivery of the pushed data after it leaves the JMAP server.
</t>

<section anchor="pushsubscriptionset" title="PushSubscription/set">
<t>Each session may only have a single push subscription registered. The push subscription is tied to the access token used to create it. Should the access token expire or be revoked, the push subscription MUST be removed by the JMAP server. The client MUST re-register the push subscription after reauthenticating to resume callbacks.
</t>
<t>To set the push subscription, make a call to <spanx style="emph">PushSubscription/set</spanx>. It takes the following argument:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">pushSubscription</spanx>: <spanx style="verb">PushSubscription|null</spanx>
The PushSubscription object representing the endpoint the JMAP server will POST events to. This will replace any previously set subscription. Set to <spanx style="verb">null</spanx> to remove any previously registered subscription.</t>
</list>
</t>
<t>The response has no arguments.
</t>
<t>The following additional errors may be returned instead of the <spanx style="emph">PushSubscription/set</spanx> response:
</t>
<t><spanx style="verb">invalidUrl</spanx>: Returned if the URL does not begin with <spanx style="verb">https://</spanx>, or is otherwise syntactically invalid or does not resolve.
</t>
<t><spanx style="verb">forbidden</spanx>: Returned if the URL is valid, but for policy reasons the server is not willing to connect to it.
</t>
</section>

<section anchor="pushsubscriptionget" title="PushSubscription/get">
<t>To check the currently set push subscription (if any), make a call to <spanx style="emph">PushSubscription/set</spanx>. It does not take any arguments. The response has a single argument:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">pushSubscription</spanx>: <spanx style="verb">PushSubscription|null</spanx>
The PushSubscription object the JMAP server is currently posting push events to, or <spanx style="verb">null</spanx> if none.</t>
</list>
</t>
</section>
</section>

<section anchor="event-source" title="Event Source">
<t>Clients that can hold open TCP connections can connect directly to the JMAP server to receive push notifications via a <spanx style="verb">text/event-stream</spanx> resource, as described in <eref target="http://www.w3.org/TR/eventsource/"/>. This is a long running HTTP request down which the server can push data.
</t>
<t>When a change occurs in the data on the server, it pushes an event called <spanx style="strong">state</spanx> to any connected clients, with the <spanx style="emph">StateChange</spanx> object as the data.
</t>
<t>The server SHOULD also send a new event id that encodes the entire server state visible to the user immediately after sending a <spanx style="emph">state</spanx> event. When a new connection is made to the event-source endpoint, a client following the <eref target="https://html.spec.whatwg.org/multipage/server-sent-events.html">server-sent events specification</eref> will send a Last-Event-ID HTTP header with the last id it saw, which the server can use to work out whether the client has missed some changes. If so, it SHOULD send these changes immediately on connection.
</t>
<t>The client MAY add a query parameter called <spanx style="verb">closeafter</spanx> with value <spanx style="verb">state</spanx> to the event-source resource URL when requesting the event-source resource. If set, the server MUST end the HTTP response after pushing a <spanx style="emph">state</spanx> event. This can be used by clients in environments where buffering proxies prevent the pushed data from arriving immediately, or indeed at all, when operating in the usual mode.
</t>
<t>The client MAY add a query parameter called <spanx style="verb">ping</spanx>, with a positive integer value representing a length of time in seconds, e.g. <spanx style="verb">ping=300</spanx>. If set, the server MUST send an event called <spanx style="strong">ping</spanx> whenever this time elapses since the previous event was sent. This MUST NOT set a new event id.
</t>
<t>The server MAY modify the interval given as a query parameter to be subject to a minimum and/or maximum value. For interoperability, servers MUST NOT have a minimum allowed value higher than 30 or a maximum allowed value less than 300.
</t>
<t>The data for the ping event MUST be a JSON object containing an <spanx style="emph">interval</spanx> property, the value (type <spanx style="verb">Number</spanx>) being the interval in seconds the server is using to send pings (this may be different to the requested value if the server clamped it to be within a min/max value).
</t>
<t>Clients can monitor for the <spanx style="emph">ping</spanx> event to help determine when the closeafter mode may be required.
</t>
<t>Refer to the JMAP Session resource section of this spec for details on how to get the URL for the event-source resource. Requests to the resource MUST be authenticated.
</t>
<t>A client MAY hold open multiple connections to the event-source resource, although it SHOULD try to use a single connection for efficiency.
</t>
</section>
</section>

<section anchor="security-considerations" title="Security considerations">

<section anchor="transport-confidentiality" title="Transport confidentiality">
<t>All HTTP requests MUST use <xref target="RFC5246"/> TLS (https) transport to ensure the confidentiality of data sent and received via JMAP. Clients MUST validate TLS certificate chains to protect against man-in-the-middle attacks.
</t>
</section>

<section anchor="authentication-scheme" title="Authentication scheme">
<t>A number of HTTP authentication schemes have been standardised (<eref target="https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml"/>). Servers should take care to assess the security characteristics of different schemes in relation to their needs when deciding what to implement.
</t>
<t>If offering the Basic authentication scheme, services are strongly recommended  to not allow a user's regular password but require generation of a unique &quot;app password&quot; via some external mechanism for each client they wish to connect. This allows connections from different devices to be differentiated by the server, and access to be individually revoked.
</t>
</section>

<section anchor="service-autodiscovery-1" title="Service autodiscovery">
<t>Unless secured by something like DNSSEC, autodiscovery of server details is
vulnerable to a DNS poisoning attack leading to the client talking to an attacker's server instead of the real JMAP server. The attacker may then man-in-the-middle requests and depending on the authentication scheme, steal credentials to generate its own requests.
</t>
<t>Clients that do not support SRV lookups are likely to try just using the <spanx style="verb">/.well-known/jmap</spanx> path directly against the domain of the username over HTTPS. Servers SHOULD ensure this path resolves or redirects to the correct JMAP Session resource to allow this to work. If this is not feasible, servers MUST ensure this path cannot be controlled by an attacker, as again it may be used to steal credentials.
</t>
</section>

<section anchor="json-parsing" title="JSON parsing">
<t>The security considerations of <xref target="RFC7159"/> apply to the use of JSON as the data interchange format.
</t>
</section>

<section anchor="denial-of-service" title="Denial of service">
<t>A small request may result in a very large response, and require considerable
work on the server if resource limits are not enforced. JMAP provides mechanisms for advertising and enforcing a wide variety of limits for mitigating this threat, including limits on number of objects fetched in a single method call, number of methods in a single request, number of concurrent requests, etc.
</t>
<t>JMAP servers MUST implement sensible limits to mitigate against resource exhaustion attacks.
</t>
</section>

<section anchor="push-encryption" title="Push encryption">
<t>When data changes, a small object is pushed with the new state strings for the types that have changed. While the data here is minimal, a passive man-in-the-middle attacker may be able to gain useful information. To ensure confidentiality, if the push is sent via a third party outside of the control of the client and JMAP server the client MUST specify encryption keys when establishing the PushSubscription.
</t>
<t>The privacy and security considerations of <xref target="RFC8030"/> and <xref target="RFC8291"/> also all apply to the use of the PushSubscription mechanism.
</t>
</section>
</section>

</middle>
<back>
<references title="Normative References">
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2047.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2231.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3339.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3629.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4648.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4790.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5051.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5246.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5785.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6186.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6570.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6764.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6838.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6901.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7159.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7230.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7235.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7493.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.8030.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.8291.xml"?>
</references>

</back>
</rfc>
