--- title: "Identity Service API" weight: 40 type: docs --- The Matrix client-server and server-server APIs are largely expressed in Matrix user identifiers. From time to time, it is useful to refer to users by other ("third-party") identifiers, or "3PID"s, e.g. their email address or phone number. This Identity Service Specification describes how mappings between third-party identifiers and Matrix user identifiers can be established, validated, and used. This description technically may apply to any 3PID, but in practice has only been applied specifically to email addresses and phone numbers. ## General principles The purpose of an identity server is to validate, store, and answer questions about the identities of users. In particular, it stores associations of the form "identifier X represents the same user as identifier Y", where identities may exist on different systems (such as email addresses, phone numbers, Matrix user IDs, etc). The identity server has some private-public keypairs. When asked about an association, it will sign details of the association with its private key. Clients may validate the assertions about associations by verifying the signature with the public key of the identity server. In general, identity servers are treated as reliable oracles. They do not necessarily provide evidence that they have validated associations, but claim to have done so. Establishing the trustworthiness of an individual identity server is left as an exercise for the client. 3PID types are described in [3PID Types](/appendices#3pid-types) Appendix. ## API standards The mandatory baseline for identity server communication in Matrix is exchanging JSON objects over HTTP APIs. HTTPS is required for communication. All `POST` and `PUT` endpoints, with the exception (for historical reasons) of [`POST /_matrix/identity/v2/account/logout`](#post_matrixidentityv2accountlogout), require the client to supply a request body containing a (potentially empty) JSON object. Clients should supply a `Content-Type` header of `application/json` for all requests with JSON bodies, but this is not required. Similarly, all endpoints require the server to return a JSON object. Servers must include a `Content-Type` header of `application/json` for all JSON responses. All JSON data, in requests or responses, must be encoded using UTF-8. ### Standard error response All identity service API endpoints MUST return error responses conforming to the [standard error response](/appendices#standard-error-response) schema. In addition to the standard error codes listed in the appendix, the following standard error codes are specific to the the identity service API: `M_MISSING_PARAMS` The request was missing one or more parameters. `M_INVALID_PARAM` The request contained one or more invalid parameters. `M_SESSION_NOT_VALIDATED` The session has not been validated. `M_NO_VALID_SESSION` A session could not be located for the given parameters. `M_SESSION_EXPIRED` The session has expired and must be renewed. `M_INVALID_EMAIL` The email address provided was not valid. `M_EMAIL_SEND_ERROR` There was an error sending an email. Typically seen when attempting to verify ownership of a given email address. `M_INVALID_ADDRESS` The provided third-party address was not valid. `M_SEND_ERROR` There was an error sending a notification. Typically seen when attempting to verify ownership of a given third-party address. ## Privacy Identity is a privacy-sensitive issue. While the identity server exists to provide identity information, access should be restricted to avoid leaking potentially sensitive data. In particular, being able to construct large-scale connections between identities should be avoided. To this end, in general APIs should allow a 3PID to be mapped to a Matrix user identity, but not in the other direction (i.e. one should not be able to get all 3PIDs associated with a Matrix user ID, or get all 3PIDs associated with a 3PID). ## Web browser clients It is realistic to expect that some clients will be written to be run within a web browser or similar environment. In these cases, the identity server should respond to pre-flight requests and supply Cross-Origin Resource Sharing (CORS) headers on all requests. When a client approaches the server with a pre-flight (OPTIONS) request, the server should respond with the CORS headers for that route. The recommended CORS headers to be returned by servers on all requests are: Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization ## API Version check {{% http-api spec="identity" api="versions" %}} ## Authentication Most endpoints in the Identity Service API require authentication in order to ensure that the requesting user has accepted all relevant policies and is otherwise permitted to make the request. Identity Servers use a scheme similar to the Client-Server API's concept of access tokens to authenticate users. The access tokens provided by an Identity Server cannot be used to authenticate Client-Server API requests. Access tokens may be provided via a request header, using the Authentication Bearer scheme: `Authorization: Bearer TheTokenHere`. Clients may alternatively provide the access token via a query string parameter: `access_token=TheTokenHere`. This method is deprecated to prevent the access token being leaked in access/HTTP logs and SHOULD NOT be used by clients. Identity Servers MUST support both methods. {{% boxes/note %}} {{% changed-in v="1.11" %}} Sending the access token as a query string parameter is now deprecated. {{% /boxes/note %}} When credentials are required but missing or invalid, the HTTP call will return with a status of 401 and the error code `M_UNAUTHORIZED`. {{% http-api spec="identity" api="v2_auth" %}} ## Terms of service Identity Servers are encouraged to have terms of service (or similar policies) to ensure that users have agreed to their data being processed by the server. To facilitate this, an identity server can respond to almost any authenticated API endpoint with an HTTP 403 and the error code `M_TERMS_NOT_SIGNED`. The error code is used to indicate that the user must accept new terms of service before being able to continue. All endpoints which support authentication can return the `M_TERMS_NOT_SIGNED` error. When clients receive the error, they are expected to make a call to `GET /terms` to find out what terms the server offers. The client compares this to the `m.accepted_terms` account data for the user (described later) and presents the user with option to accept the still-missing terms of service. After the user has made their selection, if applicable, the client sends a request to `POST /terms` to indicate the user's acceptance. The server cannot expect that the client will send acceptance for all pending terms, and the client should not expect that the server will not respond with another `M_TERMS_NOT_SIGNED` on their next request. The terms the user has just accepted are appended to `m.accepted_terms`. {{% event event="m.accepted_terms" %}} {{% http-api spec="identity" api="v2_terms" %}} ## Status check {{% http-api spec="identity" api="v2_ping" %}} ## Key management An identity server has some long-term public-private keypairs. These are named in a scheme `algorithm:identifier`, e.g. `ed25519:0`. When signing an association, the standard [Signing JSON](/appendices#signing-json) algorithm applies. The identity server may also keep track of some short-term public-private keypairs, which may have different usage and lifetime characteristics than the service's long-term keys. {{% http-api spec="identity" api="v2_pubkey" %}} ## Association lookup {{% http-api spec="identity" api="v2_lookup" %}} ### Client behaviour Prior to performing a lookup clients SHOULD make a request to the `/hash_details` endpoint to determine what algorithms the server supports (described in more detail below). The client then uses this information to form a `/lookup` request and receive known bindings from the server. Clients MUST support at least the `sha256` algorithm. ### Server behaviour Servers, upon receipt of a `/lookup` request, will compare the query against known bindings it has, hashing the identifiers it knows about as needed to verify exact matches to the request. Servers MUST support at least the `sha256` algorithm. ### Algorithms Some algorithms are defined as part of the specification, however other formats can be negotiated between the client and server using `/hash_details`. #### `sha256` This algorithm MUST be supported by clients and servers at a minimum. It is additionally the preferred algorithm for lookups. When using this algorithm, the client converts the query first into strings separated by spaces in the format `