REST API: How to filter to only get needed fields?

My problem is:
When getting a list of contacts mautic gives me lot of data that i wont use by default.

What if I get for example: https://mail.mymautic.com/api/contacts?search=owner:ownerusername&fields=id,firstname,email - This retrieves 0 data

But if i retrieve https://mail.mymautic.com/api/contacts?search=owner:ownerusername this retrieves 50 contacts (with unlimited rows for each one, when i only need 3 id,fn,email.

Is there a way to limit the data exposed so the queries are more performant ? Tried minimal but it didnt work either.

you can add minimal param in query string which should return less fields, but I am not aware of a way which would allow you to return just listed fields at the moment.

@mzagmajster can you explain the exact param you need to add in query? I have a post open from a long time ago on this as all I really want to get on a query is the contact id and as mentioned above it gives back all custom fields

Hi, according to this docs here: < Mautic Developer Documentation >, there is minimal parameter, although I see that the numbered of returned fields is still high. I would call it like this: https://<root-api-url>/api/contacts?minimal=1

Regards, M.

Thanks, I checked it out and tested… did not really work, got the entire payload back

also tried with minimal=true and still nothing. A pity

So I went ahead and wrote a small script that will just extract the contact ID. Still not the best as you are still getting the entire payload back, but you are able to extract just what you want, here is the example and it can be modified to your required needs:

#!/bin/bash
USERNAME="USERNAME"
PASSWORD="PASSWORD"
REQUEST_URL='https://your-mautic-url.com/api/contacts?where[0][col]=email&where[0][expr]=eq&where[0][val]=email-you@looking.for'
response=$(curl -s -u "$USERNAME:$PASSWORD" "$REQUEST_URL")
contact_id=$(echo "$response" | jq -r '.contacts[].id')
firstname=$(echo "$response" | jq -r '.contacts[].fields.core.firstname.value')
lastname=$(echo "$response" | jq -r '.contacts[].fields.core.lastname.value')

echo "Contact ID: $contact_id"
echo "Contact ID: $contact_id"
echo "First Name: $firstname"
echo "Last Name: $lastname"

This one works at server? So this could be called by api? or in top of this we should write an API? Would be great for a set of PR with this new features…

My workaround was to filter after server request and store that on redis…

1 Like