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 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.

Introduction

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:


            navigator.getDataStores('contacts').then(function(stores) {
               if (!stores.length)
                   return;
               var store = stores[0];

               // Create a contact.
               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]
               });
               
               // Add new contact to the store.
               store.insert(contact).then(function(id) {
                   console.log('Contact ' +
                      contact.name.givenNames[0] + ' ' +
                      contact.name.familyNames[0] + ' saved with id: ' +
                      id);
               }, function(error) {
                   console.error('Error: ' + error);
               });
            });

      

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.

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 DataStore interface and the DataStoreKey type identifier are defined in [[!DATASTORE]].

Security and privacy considerations

This API must be only exposed to trusted content

Access the contacts data stores

The contacts data stores can be retrieved using the getDataStores() function on Navigator, as defined in the Data Store API [[DATASTORE]], using "contacts" as label. This function returns an array of DataStore objects asynchronously that can be used to get, update or insert Contact objects.


           navigator.getDataStores('contacts').then(function(stores) {
               if (!stores.length)
                   return;
               var store = stores[0];
               // ...
           });

       

Which one is the system's default contacts datastore? the first one? Has a specific name?

Data Store value type

The values stored in contacts data stores are of type Contact.

Contact Interface

The 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.

readonly attribute DataStoreKey? id
Represents a unique identifier of the contact in the data store.
readonly attribute Date? lastUpdated
A Date element representing the date when the contact was last updated.
attribute ContactName name
An object representing the different naming attributes of the contact.
attribute ContactField[] emails
A ContactField element or set thereof containing the contact's email address(es). It maps to vCard's EMAIL attribute.
attribute DOMString[] photos
A string or set thereof representing the URI of the photo(s) of the contact. It maps to vCard's PHOTO attribute.
attribute ContactField[] urls
A ContactField element or set thereof containing the user's urls (e.g. personal blog). It maps to vCard's URL attribute.
attribute DOMString[] categories
A string or set thereof representing the category or categories associated to the contact (e.g. "family"). It maps to vCard's CATEGORIES attribute.
attribute ContactAddress[] addresses
A ContactAddress element or set thereof containing the user's physical address(es). It maps to vCard's ADR attribute
attribute ContactTelField[] phoneNumbers
A ContactTelField element or set thereof containing the user's telephone numbers. It maps to vCard's TEL attribute
attribute DOMString[] organizations
A string or set thereof representing the organization(s) the contact belongs to. It maps to vCard's ORG attribute
attribute DOMString[] jobTitles
A string or set thereof representing the contact's job title(s). It maps to vCard's TITLE attribute
attribute Date? birthday
A Date element representing the contact's birth date. It maps to vCard's BDAY attribute
attribute DOMString[] notes
A string or set thereof specifying supplemental information or a comment that is associated with the contact. It maps to vCard's NOTE attribute
attribute ContactField[] impp
A ContactField element or set thereof containing the user's instant messaging address(es). It maps to vCard's IMPP attribute
attribute Date? anniversary
A Date element representing the contact's anniversary. It maps to vCard's ANNIVERSARY attribute
attribute ContactGender gender
A string representing the contact's gender. It maps to the first component of vCard's GENDER attribute.

Steps

The Contact interface's contructor when invoked MUST run the following steps:

  1. Let contact be a new Contact object.
  2. Make a request to the system to generate a new unique contact identifier.
  3. Set the id attribute of contact to the generated contact identifier.
  4. Set the lastUpdated attribute of contact to the Date at which the constructor was invoked.
  5. Set the other attributes of contact to the value of the corresponding element in the contactInitDict dictionary, if present.

ContactInit Dictionary

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

ContactField Interface

The ContactField interface represents a user's attribute and the types associated to it.

attribute DOMString[] types
Indicates the types of this contact field (e.g. "home", "work").
attribute boolean preferred
Indicates whether this is the preferred contact field.
attribute DOMString value
A string that contains the user's address.

ContactFieldInit Dictionary

sequence<DOMString> types
boolean preferred
DOMString value

ContactTelField Interface

The 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.

attribute DOMString? carrier
Indicates the carrier providing service to the telephony subscription associated to that number

ContactTelFieldInit Dictionary

DOMString carrier

ContactAddress Interface

The ContactAddress interface represents a user's physical address and the types associated to it. This interface is based on vCard 4.0 ADR attribute.

attribute DOMString[] types
Indicates the types of this contact field (e.g. "home", "work").
attribute boolean preferred
Indicates whether this is the preferred contact field.
attribute DOMString streetAddress
A string that contains the name of the street. It maps to the third component in vCard's ADR attribute.
attribute DOMString locality
A string that contains the name of the locality. It maps to the forth component in vCard's ADR attribute.
attribute DOMString region
A string that contains the name of the region. It maps to the fifth component in vCard's ADR attribute.
attribute DOMString postalCode
A string that contains the postal code. It maps to the sixth component in vCard's ADR attribute.
attribute DOMString countryName
A string that contains the name of the country. It maps to the seventh component in vCard's ADR attribute.

ContactAddressInit Dictionary

sequence<DOMString> types
boolean preferred
DOMString streetAddress
DOMString locality
DOMString region
DOMString postalCode
DOMString countryName

The ContactGender enum

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 Interface

The ContactName interface represents a user's naming attributes.

attribute DOMString displayName
A string representing the contact's display name. It maps to vCard's FN attribute.
attribute DOMString[] honorificPrefixes
A string or set thereof representing the contact's honorific prefix(es). It maps to the forth component in vCard's N attribute.
attribute DOMString[] givenNames
A string or set thereof representing the contact's given name(s). It maps to the second component in vCard's N attribute.
attribute DOMString[] additionalNames
A string or set thereof representing any additional name of the contact. It maps to the third component in vCard's N attribute.
attribute DOMString[] familyNames
A string or set thereof representing the contact's family name(s). It maps to the first component in vCard's N attribute.
attribute DOMString[] honorificSuffixes
A string or set thereof representing the contact's honorific suffix(es). It maps to the fifth component in vCard's N attribute.
attribute DOMString[] nicknames
A string or set thereof representing the contact's nick name(s). It maps to vCard's NICKNAME attribute.

ContactNameInit Dictionary

DOMString displayName
sequence<DOMString> honorificPrefixes
sequence<DOMString> givenNames
sequence<DOMString> additionalNames
sequence<DOMString> familyNames
sequence<DOMString> honorificSuffixes
sequence<DOMString> nicknames

Acknowledgements

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.