<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to Verify a Valid apiKey at runTime in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/how-to-verify-a-valid-apikey-at-runtime/m-p/1167120#M4122</link>
    <description>&lt;P&gt;I am finally moving our apps to use the &lt;A href="https://developers.arcgis.com/documentation/mapping-apis-and-services/maps/services/basemap-layer-service/" target="_self"&gt;new vector basemap services&lt;/A&gt; and have set up an &lt;A href="https://developers.arcgis.com/documentation/mapping-apis-and-services/security/tutorials/create-and-manage-an-api-key/" target="_self"&gt;ESRI API key&lt;/A&gt; to access the data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to write a check at runtime that ensures that the token supplied for my app will indeed work. The logic I'm thinking is something like:&lt;/P&gt;&lt;P&gt;At Build: load api key token (from environment variables)&lt;/P&gt;&lt;P&gt;At Runtime: do a basic check that token works (isn't expired, lacking credits, works with referrer, supports the services I want to use it against (e.g. search, basemaps) etc)&lt;/P&gt;&lt;P&gt;- Make an `esriRequest` to some url endpoint that returns a brief description validating token or no and what services it works against.&lt;/P&gt;&lt;P&gt;- If token is invalid handle accordingly, fall back to other basemaps, disable search etc.&lt;/P&gt;&lt;P&gt;However I don't see anything anywhere in the ESRI API documentation discussing any such process. Of course I could test trying to fetch a particular basemap tile and see if an error message comes back but I'd rather just have a lightweight explicit "verify token" endpoint not just for a basemap but for the token itself.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I tried so far:&lt;/P&gt;&lt;P&gt;This'll work and fail with a bad token, but would like something a bit more descriptive:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;require([
  "esri/config",
  "esri/request",
  "esri/Map",
  "esri/views/MapView",
  "esri/widgets/Search",
], function (esriConfig, esriRequest, Map, MapView, Search) {
   esriConfig.apiKey = "some-invalid-key-here";
   
    esriRequest(
    "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/ArcGIS:Streets",
    {
      responseType: "json",
    }
  )
    .then(function (response) {
      let data = response.data;
      console.log(data);
    })
    .catch((err) =&amp;gt; {
      console.log("whoops");
    });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wondering if anyone has any advice before I go bother ESRI Canada support and/or make an idea suggestion about this. I also welcome feedback as to why my above approach idea might not be a good idea or is ill-advised.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 22 Apr 2022 17:39:31 GMT</pubDate>
    <dc:creator>RyanSutcliffe</dc:creator>
    <dc:date>2022-04-22T17:39:31Z</dc:date>
    <item>
      <title>How to Verify a Valid apiKey at runTime</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/how-to-verify-a-valid-apikey-at-runtime/m-p/1167120#M4122</link>
      <description>&lt;P&gt;I am finally moving our apps to use the &lt;A href="https://developers.arcgis.com/documentation/mapping-apis-and-services/maps/services/basemap-layer-service/" target="_self"&gt;new vector basemap services&lt;/A&gt; and have set up an &lt;A href="https://developers.arcgis.com/documentation/mapping-apis-and-services/security/tutorials/create-and-manage-an-api-key/" target="_self"&gt;ESRI API key&lt;/A&gt; to access the data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to write a check at runtime that ensures that the token supplied for my app will indeed work. The logic I'm thinking is something like:&lt;/P&gt;&lt;P&gt;At Build: load api key token (from environment variables)&lt;/P&gt;&lt;P&gt;At Runtime: do a basic check that token works (isn't expired, lacking credits, works with referrer, supports the services I want to use it against (e.g. search, basemaps) etc)&lt;/P&gt;&lt;P&gt;- Make an `esriRequest` to some url endpoint that returns a brief description validating token or no and what services it works against.&lt;/P&gt;&lt;P&gt;- If token is invalid handle accordingly, fall back to other basemaps, disable search etc.&lt;/P&gt;&lt;P&gt;However I don't see anything anywhere in the ESRI API documentation discussing any such process. Of course I could test trying to fetch a particular basemap tile and see if an error message comes back but I'd rather just have a lightweight explicit "verify token" endpoint not just for a basemap but for the token itself.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I tried so far:&lt;/P&gt;&lt;P&gt;This'll work and fail with a bad token, but would like something a bit more descriptive:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;require([
  "esri/config",
  "esri/request",
  "esri/Map",
  "esri/views/MapView",
  "esri/widgets/Search",
], function (esriConfig, esriRequest, Map, MapView, Search) {
   esriConfig.apiKey = "some-invalid-key-here";
   
    esriRequest(
    "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/ArcGIS:Streets",
    {
      responseType: "json",
    }
  )
    .then(function (response) {
      let data = response.data;
      console.log(data);
    })
    .catch((err) =&amp;gt; {
      console.log("whoops");
    });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wondering if anyone has any advice before I go bother ESRI Canada support and/or make an idea suggestion about this. I also welcome feedback as to why my above approach idea might not be a good idea or is ill-advised.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2022 17:39:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/how-to-verify-a-valid-apikey-at-runtime/m-p/1167120#M4122</guid>
      <dc:creator>RyanSutcliffe</dc:creator>
      <dc:date>2022-04-22T17:39:31Z</dc:date>
    </item>
  </channel>
</rss>

