This message was deleted.
# community-help
s
This message was deleted.
s
node js
j
Could you post a fully reproducible example code snippet?
s
async function generateScopedSearch() { try { const scopedKey = await client.keys().create(adminKey); const keyWithSearchPermissions = scopedKey.value; console.log('Scoped Search Key:', keyWithSearchPermissions); let key = await client.keys().generateScopedSearchKey(keyWithSearchPermissions, { 'filter_by': 'active:true' }) headers["X-TYPESENSE-API-KEY"] = key console.log(headers) console.log(key) // console.log(key) return key; } catch (error) { console.error('Error generating scoped search key:', error); } }
const adminKey = { description: 'API Key', actions: ['documents:search'], collections: ['*'], expires_at: expiryTime ? Math.floor(Date.now() / 1000) + exp : Math.floor(Date.now() / 1000) + 100, };
after calling it again after expiry forbiden key error
j
after calling it again after expiry
Once the key expires, then it can't be used... Unless I misunderstood that statement
s
once its expired i generate another but it still gve me the forbidden api
j
Copy code
expiryTime ? Math.floor(Date.now() / 1000) + exp : Math.floor(Date.now() / 1000) + 100
This will expire it in 100 seconds from creation no?
Also
exp
is undefined in that code snippet, so may be it's expiring it right away
s
const expiresAt = new Date(now.getTime() + 5 * 1000); // Add 5 minutes in milliseconds const expiresAtUnixTimestamp = Math.floor(expiresAt / 1000) console.log(expiresAtUnixTimestamp) adminKey.expires_at = expiresAtUnixTimestamp;
j
Let's do this. Could you do a
GET /keys
and post the output?
s
sure
Copy code
{
  "keys": [
    {
      "actions": [
        "documents:search"
      ],
      "collections": [
        "*"
      ],
      "description": "API Key",
      "expires_at": 1694540963,
      "id": 2210,
      "value_prefix": "pVIB"
    }
  ]
}
j
Ok so that key is about 8 minutes away before it expires
Could you use curl to make an API request with that API key directly?
s
once it expires i will share it again
j
Didn't get you... That key will only work for the next 8 minutes. After that it will not work
s
after that i generate another but that also dosent seem to work
giving forbidden error
j
generate another
Generate another parent search API key by making a call to
POST /keys
correct?
s
async function generateScopedSearch() { try { const scopedKey = await client.keys().create(adminKey); const keyWithSearchPermissions = scopedKey.value; console.log('Scoped Search Key:', keyWithSearchPermissions); let key = await client.keys().generateScopedSearchKey(keyWithSearchPermissions, { 'filter_by': 'active:true' }) headers["X-TYPESENSE-API-KEY"] = key console.log(headers) console.log(key) // console.log(key) return key; } catch (error) { console.error('Error generating scoped search key:', error); } } using this method
j
Ah ok. So that's the issue. The expiration of the scoped search api key, is tied to the expiration of the parent search api key
If the parent search API key expires then all scoped API keys generated with it, will expire as well
s
oh so i need to expire the parent search api key as well
j
The earlier expiration date takes precedence, either in the scoped api key or in the parent api key
s
but once it expires it creates the new scoped key also
Copy code
{
  "keys": [
    {
      "actions": [
        "documents:search"
      ],
      "collections": [
        "*"
      ],
      "description": "API Key",
      "expires_at": 1694540963,
      "id": 2210,
      "value_prefix": "pVIB"
    },
    {
      "actions": [
        "documents:search"
      ],
      "collections": [
        "*"
      ],
      "description": "API Key",
      "expires_at": 1694540963,
      "id": 2211,
      "value_prefix": "VP6i"
    },
    {
      "actions": [
        "documents:search"
      ],
      "collections": [
        "*"
      ],
      "description": "API Key",
      "expires_at": 1694540963,
      "id": 2212,
      "value_prefix": "OtJj"
    }
  ]
}
see i generated but still i am getting forbidden
j
Right, but after you generate a new parent api key, you then have to also regenerate the scoped search api keys again with this new parent api key
The old scoped api keys you had previously generated were based on the old parent api key which has already expired.
s
"keys": [ { "actions": [ "documents:search" ], "collections": [ "*" ], "description": "API Key", "expires_at": 1694540963, "id": 2210, "value_prefix": "pVIB" }, { "actions": [ "documents:search" ], "collections": [ "*" ], "description": "API Key", "expires_at": 1694540963, "id": 2211, "value_prefix": "VP6i" }, { "actions": [ "documents:search" ], "collections": [ "*" ], "description": "API Key", "expires_at": 1694540963, "id": 2212, "value_prefix": "OtJj" } ] these are all expired
cause evey time the search permission is getting generated and with the help of the that i generate new scoped key
j
I'm not sure I fully understand the scenario yet. To simplify, could you give me a set of curl commands (using the code snippets in the Shell tab in the docs), (something like this) that reproduces the issue step-by-step?
✅ 1