<@U04UQ68083H> How can I use Ory API Keys with the...
# ory-copilot
b
@chilly-king-10285 How can I use Ory API Keys with the javascript sdk? I'm currently trying to instantiate the identitiesClient like this:
Copy code
const identityClient = new sdk.IdentityApi({
  apiKey: process.env.KRATOS_API_KEY,
  basePath: process.env.KRATOS_ADMIN_URL,
  isJsonMime,
});
And then I'm trying to run the following code:
Copy code
const response = await identityClient.getIdentity({ id });
However, I'm getting a 401 from Ory. I double checked the api key is correct and the admin url is also correct.
c
A message from kapa.ai
The 401 error you're encountering typically indicates an issue with authorization. In the context of Ory, this could mean that your API key is not being correctly included in your requests. When using the Ory API, the API key should be included in the
Authorization
header of your requests. However, in the code you've provided, it seems like you're trying to pass the API key as a parameter when instantiating the
IdentityApi
client. This might be the reason why you're encountering a 401 error. Here's how you can include the API key in your requests:
Copy code
javascript GET /admin/identities HTTP/1.1   Host: $<http://PROJECT_SLUG.projects.oryapis.com|PROJECT_SLUG.projects.oryapis.com>   Accept: application/json   Authorization: Bearer $API_KEY
In this example,
$API_KEY
should be replaced with your actual API key. Note that the API key should be prefixed with
Bearer
in the
Authorization
header. Also, remember that API keys are bound to a specific Ory Network project and not to a specific user. This means that any user with access to the API key can perform the operations that the API key allows, even if they are no longer part of the project. Therefore, it's important to keep your API keys secure and rotate them if necessary. For more information on how to use API keys with the Ory API, you can refer to the following documentation: - [Authorizing Ory Network Project Admin APIs](https://www.ory.sh/docs/concepts/personal-access-token#authorizing-ory-network-project-admin-apis) - [Ory APIs](https://www.ory.sh/docs/getting-started/local-development#ory-apis) - [Authorizing Ory Network Workspace Admin APIs](https://www.ory.sh/docs/concepts/personal-access-token#usage-1)
b
@chilly-king-10285 Thanks. However, I don't control how the parameter is gonna passed. I'm restricted to the SDK params.
c
A message from kapa.ai
New message text here
b
That's the correct answer. I had to add the missing Authorization header. Seems like a bug though. I'd expect the SDK to do that automatically by simply using my
apiKey
. Anyway, thanks Kapa!