Copyright © 2014 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.
This specification defines a System Level API which offers a simple interface to manage user's contacts stored in the system's address book. A typical use case of the Contacts API is the implementation of an application to manage said address book.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document defines a System Level API to manage the user's contacts that are stored in the system's address book. Future versions of this specification are expected to align the contact data model with the Contacts API being defined by the Device APIs Working Group.If you find any issue with this specification, please file a bug on Github.
This document was published by the System Applications Working Group as an Editor's Draft. If you wish to make comments regarding this document, please send them to public-sysapps@w3.org (subscribe, archives). All comments are welcome.
Publication as an Editor's Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 1 August 2014 W3C Process Document.
Navigator InterfaceContactsManager InterfaceContactFindOptions DictionaryContactField InterfaceContactTelField InterfaceContactAddress InterfaceContactGender enumContactName InterfaceContact InterfaceContactsChangeEvent InterfaceThis section is non-normative.
The Contacts API allows to manage (create, edit, remove, etc) user's contacts stored in the system's address book, and thus provides the functionality needed to implement an application to manage said address book.
An example of use is provided below:
var contactName = new ContactName({ givenNames: ['John'], familyNames: ['Doe'] }); var mobilePhone = new ContactTelField({ types: ['home'], preferred: true, value: '+34698765432' }); var contact = new Contact({ name: contactName, phoneNumbers: [mobilePhone] }); navigator.contacts.save(contact).then( function(contact) { window.console.log('Contact ' + contact.name.givenNames[0] + ' ' + contact.name.familyNames[0] + ' saved!'); }, function(error) { window.console.error('Error: ' + error); } )
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key word MUST is to be interpreted as described in [RFC2119].
This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.
Implementations that use ECMAScript to implement the APIs defined in this specification MUST implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [WEBIDL], as this specification uses that specification and terminology.
The 
      EventHandler interface represents a callback used for event
      handlers as defined in [HTML5].
      
      
The concepts queue a task and fire a simple event are defined in [HTML5].
The terms event handler and event handler event types are defined in [HTML5].
The Promise interface, the concepts of a resolver, a resolver's fulfill algorithm and a resolver's reject algorithm are defined in [DOM4].
This API must be only exposed to trusted content
ContactsManager InterfaceThe ContactsManager interface exposes the contacts management
      functionality.
      
      
interface ContactsManager : EventTarget {
    Promise find (optional ContactFindOptions options);
    Promise clear ();
    Promise save (Contact contact);
    Promise remove (DOMString contactId);
                attribute EventHandler oncontactschange;
};oncontactschange of type EventHandler,            clearPromise that will allow the caller to
        be notified about the result of the operation. 
        Promisefindoptions parameter.
        It returns a Promise that will allow the caller to
        be notified about the result of the operation. 
        
        | Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| options |  | ✘ | ✔ | 
               Set of criteria that a contact needs to match to be included in
               the outcomes of the find
               operation. 
              | 
PromiseremovePromise that will allow the caller to
        be notified about the result of the operation. 
        
        | Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| contactId | DOMString | ✘ | ✘ | 
               The identifier of the Contact object that is
               requested to be removed from the address book.
              | 
Promisesaveid, already exists in the address book, it
        will be updated.
        It returns a Promise that will allow the caller to
        be notified about the result of the operation. 
        
        | Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| contact |  | ✘ | ✘ | 
               The Contact object that is requested to be saved in
               the address book.
              | 
Promise The find method when invoked MUST run the
      following steps:
        
Promise object
          and resolver its associated resolver.
          options parameter.
          Contact objects providing the results of the
            find operation.
            value argument.
          The clear method when invoked MUST run the
      following steps:
        
Promise object
          and resolver its associated resolver.
           The save method when invoked MUST run the
      following steps:
        
Promise object
          and resolver its associated resolver.
          Contact object passed as parameter.
          Contact object
            as returned by the system, and which therefore has its
            id and lastUpdated parameters set, whereas
            they could be null in the original Contact
            object passed as parameter, for instance if it is a brand new
            Contact object not yet stored in the address
            book.
            value argument.
          The remove method when invoked MUST run the
      following steps:
        
Promise object
          and resolver its associated resolver.
          Contact object identified by the
          contactId passed as parameter from the address book.
           Upon a change in a contact or set thereof is performed (i.e.
      contact(s) added / modified / removed) the system MUST fire a simple
      event named contactschange that implements the
      ContactsChangeEvent interface at the
      ContactsManager object
      
The following are the event handlers (and their corresponding
        event handler event types) that MUST be supported as attributes
        by the ContactsManager object:
        
        
| event handler | event handler event type | 
|---|---|
oncontactschange | 
              contactschange | 
            
The event named contactschange MUST implement the
    ContactsChangeEvent interface.
    
ContactFindOptions DictionaryThe ContactFindOptions dictionary represents the criteria used
      to select the contacts to be returned by a find operation.
      
dictionary ContactFindOptions {
    DOMString           value;
    FilterOperator      operator;
    sequence<DOMString> fields;
    sequence<DOMString> sortBy;
    SortOrder           sortOrder;
    unsigned long       resultsLimit;
};ContactFindOptions Membersfields of type sequence<DOMString>operator of type FilterOperatorresultsLimit of type unsigned longsortBy of type sequence<DOMString>sortOrder of type SortOrdervalue of type DOMStringThere are 2 possible filter operations:
enum FilterOperator {
    "contains",
    "is"
};| Enumeration description | |
|---|---|
contains | With this operation, a contact will be returned if its filtered field contains the given value. | 
is | With this operation, a contact will be returned if its filtered field matches exactly the given value. | 
The returned contacts can be sorted in one of the following orders:
enum SortOrder {
    "ascending",
    "descending"
};| Enumeration description | |
|---|---|
ascending | The contacts will be sorted in ascending order. | 
descending | The contacts will be sorted in descending order. | 
It is for further study whether the level of flexibility of the filters needs to be increased and/or additional mechanisms need to be put in place so that applications can keep a local copy of the address book and perform the filtering locally (e.g. startTrackingChanges() and getNewChanges() methods)
ContactField InterfaceThe ContactField interface represents a user's attribute and the
      types associated to it.
      
[Constructor(optional ContactFieldInit initDict)]
interface ContactField {
                attribute DOMString[] types;
                attribute boolean     preferred;
                attribute DOMString   value;
};preferred of type boolean,            types of type array of DOMString,            value of type DOMString,            ContactFieldInit Dictionarydictionary ContactFieldInit {
    sequence<DOMString> types;
    boolean             preferred;
    DOMString           value;
};ContactFieldInit Memberspreferred of type booleantypes of type sequence<DOMString>value of type DOMStringContactTelField InterfaceThe ContactTelField interface represents a telephone number as
      well as metadata associated to it, namely the types (e.g. "voice", "text")
      and the carrier providing service to the telephony subscription associated
      to that number.
      
[Constructor(optional ContactTelFieldInit initDict)]
interface ContactTelField : ContactField {
                attribute DOMString? carrier;
};carrier of type DOMString,            , nullableContactTelFieldInit Dictionarydictionary ContactTelFieldInit : ContactFieldInit {
    DOMString carrier;
};ContactTelFieldInit Memberscarrier of type DOMStringContactAddress InterfaceThe ContactAddress interface represents a user's physical
      address and the types associated to it. This interface is based on vCard 4.0 ADR attribute.
      
[Constructor(optional ContactAddressInit initDict)]
interface ContactAddress {
                attribute DOMString[] types;
                attribute boolean     preferred;
                attribute DOMString   streetAddress;
                attribute DOMString   locality;
                attribute DOMString   region;
                attribute DOMString   postalCode;
                attribute DOMString   countryName;
};countryName of type DOMString,            locality of type DOMString,            postalCode of type DOMString,            preferred of type boolean,            region of type DOMString,            streetAddress of type DOMString,            types of type array of DOMString,            ContactAddressInit Dictionarydictionary ContactAddressInit {
    sequence<DOMString> types;
    boolean             preferred;
    DOMString           streetAddress;
    DOMString           locality;
    DOMString           region;
    DOMString           postalCode;
    DOMString           countryName;
};ContactAddressInit MemberscountryName of type DOMStringlocality of type DOMStringpostalCode of type DOMStringpreferred of type booleanregion of type DOMStringstreetAddress of type DOMStringtypes of type sequence<DOMString>ContactGender enumenum ContactGender {
    "male",
    "female",
    "other",
    "none",
    "unknown"
};| Enumeration description | |
|---|---|
male | contact is a male. | 
female | contact is a female. | 
other | contact has another gender. | 
none | contact does not have a gender (not applicable). | 
unknown | contact's gender is unknown. | 
ContactName InterfaceThe ContactName interface represents a user's naming attributes.
      
[Constructor(optional ContactNameInit initDict)]
interface ContactName {
                attribute DOMString   displayName;
                attribute DOMString[] honorificPrefixes;
                attribute DOMString[] givenNames;
                attribute DOMString[] additionalNames;
                attribute DOMString[] familyNames;
                attribute DOMString[] honorificSuffixes;
                attribute DOMString[] nicknames;
};additionalNames of type array of DOMString,            displayName of type DOMString,            familyNames of type array of DOMString,            givenNames of type array of DOMString,            honorificPrefixes of type array of DOMString,            honorificSuffixes of type array of DOMString,            nicknames of type array of DOMString,            ContactNameInit Dictionarydictionary ContactNameInit {
    DOMString           displayName;
    sequence<DOMString> honorificPrefixes;
    sequence<DOMString> givenNames;
    sequence<DOMString> additionalNames;
    sequence<DOMString> familyNames;
    sequence<DOMString> honorificSuffixes;
    sequence<DOMString> nicknames;
};ContactNameInit MembersadditionalNames of type sequence<DOMString>displayName of type DOMStringfamilyNames of type sequence<DOMString>givenNames of type sequence<DOMString>honorificPrefixes of type sequence<DOMString>honorificSuffixes of type sequence<DOMString>nicknames of type sequence<DOMString>Contact InterfaceThe Contact interface represents a contact stored in the address
      book. As a principle the attributes are based on vCard 4.0 and reuse the
      literal used in that standard. Any naming deviation is mentioned in the
      description of the corresponding attribute. Whereas this correspondence
      facilitates the import/export from/to vCard format it should be noted that
      a vCard import/export API is out of scope of this specification as parsing
      and serializing can be efficiently done in JavaScript, and libraries are
      readily available.
      
[Constructor(optional ContactInit contactInitDict)]
interface Contact {
    readonly    attribute DOMString?        id;
    readonly    attribute Date?             lastUpdated;
                attribute ContactName       name;
                attribute ContactField[]    emails;
                attribute DOMString[]       photos;
                attribute ContactField[]    urls;
                attribute DOMString[]       categories;
                attribute ContactAddress[]  addresses;
                attribute ContactTelField[] phoneNumbers;
                attribute DOMString[]       organizations;
                attribute DOMString[]       jobTitles;
                attribute Date?             birthday;
                attribute DOMString[]       notes;
                attribute ContactField[]    impp;
                attribute Date?             anniversary;
                attribute ContactGender     gender;
};addresses of type array of ContactAddress,            ContactAddress element or set thereof containing the
        user's physical address(es). It maps to vCard's ADR attributeanniversary of type Date,            , nullableDate element representing the contact's anniversary.
        It maps to vCard's ANNIVERSARY attributebirthday of type Date,            , nullableDate element representing the contact's birth date.
        It maps to vCard's BDAY attribute
        categories of type array of DOMString,            emails of type array of ContactField,            ContactField element or set thereof containing the
        contact's email address(es). It maps to vCard's EMAIL attribute.gender of type ContactGender,            id of type DOMString, readonly   , nullableimpp of type array of ContactField,            ContactField element or set thereof containing the
        user's instant messaging address(es). It maps to vCard's IMPP
        attributejobTitles of type array of DOMString,            lastUpdated of type Date, readonly   , nullableDate element representing the date when the contact
        was last updated.
        name of type ContactName,            notes of type array of DOMString,            organizations of type array of DOMString,            phoneNumbers of type array of ContactTelField,            ContactTelField element or set thereof containing the
        user's telephone numbers. It maps to vCard's TEL attributephotos of type array of DOMString,            urls of type array of ContactField,            ContactField element or set thereof containing the
        user's urls (e.g. personal blog). It maps to vCard's URL attribute. The Contact interface's contructor when invoked MUST run the
      following steps:
       
Contact object.
        id attribute of contact to the
        generated contact identifier.
        lastUpdated attribute of contact to
        the Date at which the constructor was invoked.
        contactInitDict dictionary, if
        present.
       ContactInit Dictionarydictionary ContactInit {
    ContactName               name;
    sequence<ContactField>    emails;
    sequence<DOMString>       photos;
    sequence<ContactField>    urls;
    sequence<DOMString>       categories;
    sequence<ContactAddress>  addresses;
    sequence<ContactTelField> phoneNumbers;
    sequence<DOMString>       organizations;
    sequence<DOMString>       jobTitles;
    Date                      birthday;
    sequence<DOMString>       notes;
    sequence<ContactField>    impp;
    Date                      anniversary;
    ContactGender             gender;
};ContactInit Membersaddresses of type sequence<ContactAddress>anniversary of type Datebirthday of type Datecategories of type sequence<DOMString>emails of type sequence<ContactField>gender of type ContactGenderimpp of type sequence<ContactField>jobTitles of type sequence<DOMString>name of type ContactNamenotes of type sequence<DOMString>organizations of type sequence<DOMString>phoneNumbers of type sequence<ContactTelField>photos of type sequence<DOMString>urls of type sequence<ContactField>ContactsChangeEvent InterfaceThe ContactsChangeEvent interface represents events related to
      the set of contacts that have been simultaneously added, removed and/or
      modified. Changes that are applied to the address book in batches cause a
      ContactsChangeEvent with multiple contact references to be fired,
      whereas changes applied sequentially cause a ContactsChangeEvent
      with a single contact reference to be fired
      
[Constructor(DOMString type, optional ContactsChangeEventInit
      eventInitDict)]
interface ContactsChangeEvent : Event {
    readonly    attribute DOMString[] added;
    readonly    attribute DOMString[] modified;
    readonly    attribute DOMString[] removed;
};added of type array of DOMString, readonly   Contact object(s)
        that have been added.modified of type array of DOMString, readonly   Contact object(s)
        that have been modified.removed of type array of DOMString, readonly   Contact object(s)
        that have been removed.ContactsChangeEventInit Dictionarydictionary ContactsChangeEventInit : EventInit {
    sequence<DOMString> added;
    sequence<DOMString> modified;
    sequence<DOMString> removed;
};ContactsChangeEventInit Membersadded of type sequence<DOMString>modified of type sequence<DOMString>removed of type sequence<DOMString>The editors would like to express their gratitude to the Mozilla B2G Team for their technical guidance, implementation work and support, and specially to Tantek Çelik and Gregor Wagner, the original authors of B2G Contact API.