title: How to verify a google maps api key in html javascript?
body: <p>I have a rather standard Google Maps page, using my own API key. The page only does some marker additions by JS and presenting it in a text-box. If the key is invalid or expired or whatever, the following page is displayed.</p>
  
  <p><a href="https://i.stack.imgur.com/Tn3nD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tn3nD.png" alt="enter image description here"></a></p>
  
  <p>That is fine, but then I would like to at lest tell the user why it is  wrong. The map is accessed by standard:</p>
  
  <pre><code>...
  &lt;script type="text/javascript" 
          src="http://maps.google.com/maps/api/js?key=API_KEY"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="map"&gt;&lt;/div&gt; 
  &lt;p&gt;&lt;input type="text" "readonly" id="status" size=40&gt;&lt;/p&gt;
  ...
  </code></pre>
  
  <p>However, I would like to check that the API key is working and valid, and tell the user if the key is OK or not in that text box.</p>
  
  <p>How can I do that?</p>
  
  <p>PS. The problem with using a <code>callback</code> function in the <code>src=</code>, is that it returns regardless what is the API status, but not actually the status itself. <a href="https://www.igorkromin.net/index.php/2017/07/02/identifying-when-google-maps-successfully-authenticates-your-api-key/" rel="nofollow noreferrer">Here</a> is a kludge solution of this. </p>
  
  <hr>
  
  <p><strong>EDIT</strong>: I found an answer in the obscurely and <a href="https://developers.google.com/maps/documentation/javascript/events#auth-errors" rel="nofollow noreferrer">poorly documented</a> callback function, <code>gm_authFailure()</code>, and solved my question with:</p>
  
  <pre><code>function gm_authFailure() { 
      let apiError = "No or invalid API key!"
      document.getElementById('status').value = apiError; 
  };
  </code></pre>
  
  <p>You don't even need to specify this callback in your URL, it will use it anyway! </p>