Hi team, I’m running into an intermittent issue wh...
# general
m
Hi team, I’m running into an intermittent issue when initializing the Permit Node.js SDK. Most of the time the SDK fails, but occasionally the request succeeds.
Copy code
{"level":50,"time":"2025-11-22T09:40:20.177Z","label":"Permit.io","msg":"[undefined] permit.api.getApiKeyScope(), err: undefined"}
Error creating resource instance: PermitContextError: could not fetch the api key scope in order to set the api context level
I’ve tested this with both Cloud PDP and a local PDP, but the behavior is the same. SDK Initialization:
Copy code
const permit = new Permit({
  token: process.env.PERMIT_API_KEY || '',
  pdp: 'https://cloudpdp.api.permit.io',
  timeout: 30000,
  apiContext: {
    project: "default",
    environment: "dev",
  },
  throwOnError: true,
  log: {
    level: 'debug',
  }
});
Can you clarify what causes this intermittent failure and how to resolve it?
o
Hi this seems like a networking error. where are you running your SDK?
the connection here is to our API in the cloud.
m
Im running the SDK on my local machine (Linux) I tried the local PDP as well
Copy code
const permit = new Permit({
  token: process.env.PERMIT_API_KEY || '',
  pdp: 'http://localhost:7766',
  timeout: 30000,
  apiContext: {
    project: "default",
    environment: "dev",
  },
  throwOnError:true,
  log: {
    level: 'debug',
  }
});
o
1. The connection issue you're having is to the remote API from the SDK 2. please upgrade to the latest version of the SDK, latest version doesn't require the remote API for init
m
Currently im running version "permitio": "^2.7.5" Could you please share the latest documentation related to the updated initialization flow and SDK usage?
o
It's seems you are running the latest version. then it seems your code does need to talk to the API service.
m
I’d appreciate it if you could share some documentation on this
there's no specific documentation on networking errors. I recommend you run your code from a machine with better internet connectivity
you can see the API reference here https://docs.permit.io/api/api-reference you can also try to connect to the API directly without the SDK
also if you're using a project or org API key I'd recommend you switch them to an environment API key
@Ask Inkeep anything else you'd recommend for API access issues with the Nodejs SDK
a
Thanks for tagging me @Or Weis. I'll look through relevant sources and get back to you shortly.
Based on the error you're experiencing with
getApiKeyScope()
, here are additional recommendations for resolving API access issues with the Node.js SDK: The SDK attempts to fetch the API key scope during initialization to set the appropriate context level (1) . This process validates your API key and determines its access level (organization, project, or environment) . For troubleshooting: 1. ​*Verify your API Key type*​ - Environment API Keys are recommended for SDK usage (2) . You can check your API key type in the Permit Dashboard under Settings -> API Keys . 2. ​*Test direct API connectivity*​ - Try making a direct API call to verify connectivity:
Copy code
curl -H "Authorization: Bearer <API Key>" https://api.permit.io/v2/facts/{projectKey}/{environmentKey}/users
3. ​*Consider the API URL configuration*​ - If you're in a different region, you may need to specify the appropriate API URL:
Copy code
const permit = new Permit({
  token: "[YOUR_API_KEY]",
  pdp: "http://localhost:7766",
  // the region in which the Permit environment is located
  apiUrl: "https://api.eu.permit.io",
}) ;
4. ​*Review API Logs*​ - Check the API Log tab under Settings in the Permit Dashboard to see if your requests are reaching the API and what responses are being returned . The Response tab provides valuable error messages for debugging failed API calls . Since you mentioned this is an intermittent issue with the
getApiKeyScope()
call failing, I'd like to help our engineers investigate further. Could you please provide: 1. Which Workspace / Project / Env you are using 2. What calls you are performing (as SDK code or CURL) 3. Which PDP version you are running 4. Any relevant logs from your PDP
m
Thanks You! I don’t think this is related to internet connectivity. Im already using an environment-based API key. Direct calls to the API work without any issues. Do you have a specific API URL for the APAC region?
o
No we don't have an APAC region.
You said yourself it works sporadically
that sounds like networking it's unlikely an SDK issue
m
Ill check Thank you