How to create a contact via API/HTTP Request

Hi all, I am not sure if this is the correct category to post my issue.

I am really struggling since yesterday to add contacts into my nextcloud server via
n8n / API / http request

I would really appreciate if you guys can guide me on how to setup that.

Something like this:
make a post request to your nextcloud URL
include in the header ( username and password )
include in the body json field with the contact information.

Thanks

hi @kokoko welcome to the forum :handshake:

I have no working example but this API reference might be useful:

https://docs.nextcloud.com/server/latest/developer_manual/client_apis/index.html

I don’t see any contacts related methods right away
 but maybe you are lucky.
additionally I’m under impression contacts are exposed through WebDAV - maybe you can access them this way


Appreciate your reply. But sadly, nothing useful in NC documentation.

Is it even possible? I have spent long time trying to figure it out.

Just use webdav for example with a cURL request:

curl -u <USERNAME>:<PASSWORD> 'https://<NEXTCLOUD_URL>/remote.php/dav/addressbooks/users/<USERNAME>/<ADDRESSBOOKNAME>/12345678-1234-1234-1234-1234567890AB.vcf' -X PUT -H 'Depth: 0' -H 'Content-Type: text/vcard; charset=utf-8;' -H 'DNT: 1' \
--data-binary 'BEGIN:VCARD
VERSION:3.0
FN:Example Mister
UID:12345678-1234-1234-1234-1234567890AB
ADR;TYPE=HOME:;;Examplestreet 1;Examplecity;;12345;
TEL;TYPE=HOME,VOICE:123456789
EMAIL;TYPE=HOME:mister@example.com
END:VCARD'

As filename use the same UID as in the vcf and make sure to create different UIDs for different contacts.
Add more values to your liking according to RFC 6350

1 Like

Thank you so much. It worked well.

I wonder why they hide these info from documentation.

1 Like

To create a contact via API/HTTP Request in Nextcloud, you’ll typically use the Nextcloud Contacts API https://tech-stack.com/blog/what-is-an-api/

Configure HTTP Request Node in n8n:
Drag an “HTTP Request” node onto your workflow.
Double-click on the node to configure it.
Set HTTP Request Method and URL
Choose the appropriate HTTP method, likely POST in your case.
Set the URL to your Nextcloud Contacts API endpoint. It might look something like https:// your-nextcloud-instance/remote. php/dav/addressbooks/user/addressbook/.

Configure Headers:
Add headers for authentication. For Nextcloud, you might use Basic Authentication. Add a header with the key Authorization and the value Basic base64(username:password). Replace base64(username:password) with the base64 encoding of your Nextcloud username and password concatenated with a colon (e.g., base64(‘yourUsername:yourPassword’)).

Set Body Content:
In the “Body” section, select JSON/RAW if you are sending JSON data.
Enter the JSON payload with the contact information.

Save and Execute:
Save your workflow and execute it to test the setup.
Here’s a breakdown of the JSON payload:
cards: An array of contact cards. You can have multiple cards if you want to add multiple contacts in one request.
addresses: An array of addresses associated with the contact.
emails: An array of email addresses associated with the contact.
fn: The full name of the contact.

Make sure to adapt the payload based on the specific requirements and structure of the Nextcloud Contacts API.
Please note that using basic authentication in the headers might expose your credentials if not done securely. In a production environment, consider using more secure authentication methods, such as OAuth tokens, if supported by Nextcloud.