1

I played around with the ApiExplorer for API4 While the doc says default select would be * it doesn't return all contact details. So I added the SELECT for phone_primary and email_primary and joined the phoneDB. This worked for one phonenumber, but all the other phonenumber which can be set for a contact (e.g. fax,mobile) don't show up.

Anyway, I tried to use the curl example from the ApiExplorer and it worked so far it returns all contacts and a lot senseless details. However, limit the request is not working at all. It always returns all contacts. Also phone and email are always missing no matter what I do.

My Postman request looks like this:

https://demo.circle-interactive.co.uk/civicrm/ajax/api4/Contact/get?select= ["email_primary.", "phone.", "first_name", "last_name", "phone.mobile_provider_id", "organization_name"]&join= [["Phone AS phone", "LEFT"]]&limit=5

This are my params in Postman: {"select": ["email_primary.", "phone.", "first_name", "last_name", "phone.mobile_provider_id", "organization_name"], "join": [["Phone AS phone", "LEFT"]],
"limit": 5}

This is what API-Explorer shows as valid curl example. But it's not working as curl or Postman request. As mentioned a lot senseless contact details are returned but phone and email are always missing in the result.

CRM_URL='https://demo.circle-interactive.co.uk/civicrm/ajax/api4/Contact/get' CRM_AUTH='X-Civi-Auth: Bearer MY_API_KEY'

curl -X POST -H "$CRM_AUTH" "$CRM_URL"
-d 'params=%7B%22select%22%3A%5B%22email_primary.%2a%22%2C%22phone.%2a%22%2C%22first_name%22%2C%22last_name%22%2C%22phone.mobile_provider_id%22%2C%22organization_name%22%5D%2C%22join%22%3A%5B%5B%22Phone%20AS%20phone%22%2C%22LEFT%22%5D%5D%2C%22limit%22%3A5%7D'

While emails and phonenumber are returned in the ApiExplorer for API3 with a similar request, the curl example for API3 is not working at all. (Invalid url)

I don't get it where the problem is. Are this ApiExplorer examples still up to date? Any suggestions?

2
  • It sounds like there are 2 parts to your question. StackExchange is set up to answer one question at a time, so it might have been better to ask one question about how to fetch all contact Emails and another about how to properly encode curl requests.
    – Coleman
    Commented Oct 25, 2023 at 18:45
  • Well, actually I just added all details I gathered while trying to make it work. Yes, the problem is just to get email and phone numbers when fetching a contacts I don't know If it's a problem of curl format, wrong examples or an api4 bug. Maybe the api3 api4 inconsistence behavior is another way to narrow down the problem?!?
    – Gesko
    Commented Oct 25, 2023 at 20:33

1 Answer 1

2

This looks like a 2-parter so I'll just answer your first question about how to fetch all emails & phones for a contact. Please start a new post for your curl question.

APIv4 does fetch all fields (aka SELECT *) by default, but only fields that actually belong to the entity. If you inspect the civicrm_contact table in SQL you'll see that email is not actually a column. Rather it's a join to another table.

APIv4 can do a lot of fancy stuff with joins, but you have to ask it to (if it did all the fancy stuff by default it would probably blow up your server or at least significantly slow it down).

It's all a bit too much to explain in one StackExchange answer, so I recommend you go and check out the docs on:

  1. Implicit Joins
  2. Explicit Joins

And in addition to the API Explorer, try playing with SearchKit. It's a bit more user-friendly and can generate the types of aggregate joins you wanted (e.g. fetching multiple emails per contact) with just a few clicks. Then click on the "Query Info" tab and SearchKit will show you the APIv4 params it has generated to do all the fancy stuff you asked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.