CardDAV


Reading time: about 2 minutes

This page is currently a brain dump of CardDAV related information. If there is a library for this in the language you’re using, you’re probably better off using that.

CardDAV has a lot of functionality, but depending on your purposes you can keep your code simple and get the job done easily. With critical information such as address books, it is usually a good idea to keep your access read only. If you mess up with read only access, you just read incorrectly. But if you mess up when trying to read/delete contacts, you risk losing user data.

Most of URLs on this page apply to FastMail. They may or may not work with other providers. Fortunately, CardDAV responses include the URL of every resource, so all you need to do is write your code to follow those. This way, your code will work across different services, or even when your service decides to change URL structures.

Finding the CardDAV endpoint

You can send a PROPFIND request to /.well-known/carddav. It should redirect you to the correct endpoint. You will need to be authenticated to send this request.

curl -Lv -X PROPFIND --header "Depth: 0" -u "$email:$pass" https://carddav.fastmail.com/.well-known/carddav

The response XML should contain a field like <D:href>/dav/addressbooks</D:href>, which is the CardDAV endpoint.

Using cURL to access a CardDAV server

While the programming language you’re using probably has an HTTP client, it is very useful to try things in the command line by sending requests and looking at the responses. Tools like cURL can serve this purpose.

email="webdosusb@gmail.com"
pass="supersecret"

curl -X PROPFIND --header "Depth: 1" -u "$email:$pass" https://carddav.fastmail.com/dav/addressbooks/user/$email/

Getting the address book as a single VCard

If you send a GET request to the URL of an address book, the CardDAV server will reply with all the VCard objects serialized to a single response. This can be useful for making backups or importing address books for the first time.

curl -u "$email:$pass" https://carddav.fastmail.com/dav/addressbooks/user/$email/MyAddressBook/

HTTP Methods

  • PROPFIND: This method is used when requesting information/properties about a certain object from the CardDAV server.
    • Examples: Addressbooks for a user, last modified date of a VCard object.
  • PUT: This method is used to insert an object into the CardDAV server.
    • Examples: Creating a contact and putting the VCard object into the server.
  • GET: This method is used to get objects from the CardDAV server.
    • Examples: Getting an address book, getting a VCard object

Errors

  • propfind-finite-depth: If you are seeing <D:propfind-finite-depth/> in the server response, you most likely forgot to pass a Depth header with your HTTP request. Just pass Depth: 1 and the error should disappear.

Other Resources

  • vdirsyncer - A program that can syncronise with CardDAV servers

Citation

If you find this work useful, please cite it as:
@article{yaltirakli,
  title   = "CardDAV",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/carddav"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "CardDAV", November, 2024. [Online]. Available: https://www.gkbrk.com/carddav. [Accessed Nov. 12, 2024].
APA Style
Yaltirakli, G. (2024, November 12). CardDAV. https://www.gkbrk.com/carddav
Bluebook Style
Gokberk Yaltirakli, CardDAV, GKBRK.COM (Nov. 12, 2024), https://www.gkbrk.com/carddav

Comments

Comment by Rich
2019-06-22 at 21:23
Spam probability: 4.389%

cheers, was struggling to get cURL to tell me anything!

© 2024 Gokberk Yaltirakli