title: Google Sheet App Script Error With API Calls
body: <p>I wrote a Google sheet app script that requires some API calls. When I run the script within App Script editor, it runs fine.</p>
  <p>But when I call the formula from within Google Sheets, then it throws an error saying &quot;invalid API key&quot; while I know that API is correct.</p>
  <pre><code>    const api_key = &quot;&quot;;
  
      const response = UrlFetchApp.fetch(&quot;https://serpapi.com/search.json?q=&quot; + keyword 
   +&quot;&amp;hl=&quot;+&quot;&amp;gl=&quot;+location+&quot;&amp;num=100&quot;+ &quot;&amp;engine = Google&quot; + 
  &quot;&amp;google_domain=google.co.uk&quot;+&quot;&amp;api_key=&quot;+api_key);
  </code></pre>
  <p>Here is the whole script</p>
  <pre><code>    function GetRank(keyword, domain, location) {
  
    
  
    
  
  const response = UrlFetchApp.fetch(&quot;https://serpapi.com/search.json?q=&quot; + keyword +&quot;&amp;hl=&quot;+&quot;&amp;gl=&quot;+location+&quot;&amp;num=100&quot;+ &quot;&amp;engine = Google&quot; + &quot;&amp;google_domain=google.co.uk&quot;+&quot;&amp;api_key=xxx-xxx-xxx&quot;);
  
   
  
    const json = response.getContentText();
    const results = JSON.parse(json);
    const final = results.organic_results;
    for(let i = 0; i&lt; final.length; i++){
     rank = 'Not in Top 100';
     links = final[i].link;
     if(links.includes(domain)){
       rank = final[i].position;
       show = final[i].link;
       break;
       
     }
  
    }
    console.log(rank, show);
    return [rank, show];
  
  }
  </code></pre>