Answers.Body: <p>For your <code>cv</code> call either turn off the permission check or specify a user to run with.</p>
  <p>So either:</p>
  <pre><code>&lt;?php
      $result = civicrm_api4('Contact', 'get', [
          'checkPermissions' =&gt; FALSE,
          'select' =&gt; ['id'],
          'where' =&gt; [['first_name', '=', 'Ken']],
          'limit' =&gt; 2,
      ]);
      echo $result-&gt;count(), &quot;\n&quot;;
  </code></pre>
  <p>or</p>
  <pre><code>sudo -u daemon `which cv` scr -U admin my_script.php
  </code></pre>
  <p>where <code>admin</code> is a CMS user with appropriate powers.</p>
  <p>FYI, with APIv3 <code>check_permissions</code> defaults to 0 whereas APIv4 <code>checkPermissions</code> defaults to True.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-05T10:10:10.587Z
Answers.LastActivityDate: 2021-11-05T10:18:23.407Z
Answers.LastEditDate: 2021-11-05T10:18:23.407Z
Answers.UserDisplayName: Aidan
Body: <p>I'm trying to query my data for records with a given firstname/lastname pair (see <a href="https://civicrm.stackexchange.com/questions/40627/map-firstname-lastname-pairs-to-ids">Map firstname-lastname pairs to IDs</a> for some previous context) using a couple different methods, and failing.</p>
  <h2>API4</h2>
  <p>The first way is using <code>cv</code> and some PHP code:</p>
  <pre><code>&lt;?php
  
      $result = civicrm_api4('Contact', 'get', [
          'select' =&gt; ['id'],
          'where' =&gt; [['first_name', '=', 'Ken']],
          'limit' =&gt; 2,
      ]);
      echo $result-&gt;count(), &quot;\n&quot;;
  </code></pre>
  <p>invoked via <code>sudo -u daemon `which cv` scr my_script.php</code>.</p>
  <p>This prints <code>0</code>, though there are several records in my database with the first name <code>'Ken'</code>.</p>
  <h2>REST API</h2>
  <p>The second way I've tried is to hit the REST API through some Python code, like so:</p>
  <pre><code>import requests
  
  args = dict(
      entity='Contact',
      action='get',
      json='{&quot;sequential&quot;:1,&quot;return&quot;:&quot;id&quot;,&quot;first_name&quot;:&quot;Ken&quot;}',
      api_key='my-api-key',
      key='my-site-key'
  )
  
  result = requests.get('http://my-site/modules/contrib/civicrm/extern/rest.php', params=args)
  </code></pre>
  <p>This gives me:</p>
  <pre><code>b'Could not load the settings file at: /bitnami/drupal/modules/contrib/civicrm/../..//default/civicrm.settings.php\n'
  </code></pre>
  <p>so I'm thinking my REST API might not quite be functional?  My settings file is at <code>/bitnami/drupal/sites/default/civicrm.settings.php</code>, which is not what it seems to be looking for.</p>
  <h2>Question</h2>
  <p>How can I get one of these methods working?</p>
  <h2>Details</h2>
  <p>I'm using CiviCRM 5.42.1 on Drupal 7.82.</p>
  
Tags: <api><rest><apiv4>
Title: Make API queries - two methods failing
ThreadText: # Make API queries - two methods failing
  
  (Asked by: username_0 on 2021-11-05)
  
  I'm trying to query my data for records with a given firstname/lastname pair (see Map firstname-lastname pairs to IDs for some previous context) using a couple different methods, and failing.
  API4
  The first way is using cv and some PHP code:
   ['id'],
          'where' => [['first_name', '=', 'Ken']],
          'limit' => 2,
      ]);
      echo $result->count(), "\n";
  
  invoked via sudo -u daemon `which cv` scr my_script.php.
  This prints 0, though there are several records in my database with the first name 'Ken'.
  REST API
  The second way I've tried is to hit the REST API through some Python code, like so:
  import requests
  
  args = dict(
      entity='Contact',
      action='get',
      json='{"sequential":1,"return":"id","first_name":"Ken"}',
      api_key='my-api-key',
      key='my-site-key'
  )
  
  result = requests.get('http://my-site/modules/contrib/civicrm/extern/rest.php', params=args)
  
  This gives me:
  b'Could not load the settings file at: /bitnami/drupal/modules/contrib/civicrm/../..//default/civicrm.settings.php\n'
  
  so I'm thinking my REST API might not quite be functional?  My settings file is at /bitnami/drupal/sites/default/civicrm.settings.php, which is not what it seems to be looking for.
  Question
  How can I get one of these methods working?
  Details
  I'm using CiviCRM 5.42.1 on Drupal 7.82.
  
  **Tags:** api, rest, apiv4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-11-05. Score: 2 | Accepted answer
  
  For your cv call either turn off the permission check or specify a user to run with.
  So either:
   FALSE,
          'select' => ['id'],
          'where' => [['first_name', '=', 'Ken']],
          'limit' => 2,
      ]);
      echo $result->count(), "\n";
  
  or
  sudo -u daemon `which cv` scr -U admin my_script.php
  
  where admin is a CMS user with appropriate powers.
  FYI, with APIv3 check_permissions defaults to 0 whereas APIv4 checkPermissions defaults to True.
  
  
  ---
  
  