Hey guys, I need some help on an api-key concept I...
# support-questions-legacy
n
Hey guys, I need some help on an api-key concept I'd like to build out.
r
Hey @nadilas
n
I'll be allowing users to generate api keys/tokens for themselves and I already have supertoken in place. I'd like the verifySession to grab the api-key header and turn it into a user.
My first idea was metadata, but seems off and I haven't checked yet if I can override the verifySession to do a lookup for a user based on metadata
Any ideas for me to check into?
r
I’m not sure I understand what you mean by grab the api-key and turn it into a user
n
if i had total control of the verifySession, I'd do:
Copy code
ts
const apiKey = req.headers['x-api-key']
const user = db.userByApiKey(apiKey)
const session = supertokens.newSession(user)
maybe code explains it better than i can
r
Ah I see.
You don’t need to use verifySession at all in this case. Just do what you wrote above. The result of calling createNewSession is the session object
Which you get when you call verifySession
n
Oh 😅
and can I somehow tranlaste
db.userByApiKey
to store it within supertokens? or should I keep record of all api tokens of a user in a separate database?
r
You could store the api tokens in supertokens such that it’s the ID to the usermetadata recipe and the resulting json contains the user id of the user
Cause metadata recipe can take in any random id as well. And associate any json against it
It’s a strange way to use the metadata recipe, but it would work
n
😆
it basically would just save a separate table in an external database
I do agree it's a misuse of the recipe itself, doesn't sound right
Awesome, thanks 🙂
r
Well, there is no downside in using the metadata recipe that way. As long as the api key length is leaser than 256 chars
n
It is for now. Do you have roundtrip metrics for a metadata.get call?
r
Not really. But it’s a simple select query against a primary ID of a table
So I’d imagine it being quite fast anyway
The only issue is that it would query the core which would query the db.
So 2 network calls
As opposed to if you directly query your db
n
👍 one last question, can I use the metadata recipe from an edge function?
right.
r
You can. Do supertokens.init with the recipeList containing the metadata recipe init and then use the functions in there
If you don’t wanna use the SDKs in the edge function, just query the core directly
n
we are referring to the frontend init or the backend?
r
Backend
n
got it
perfect, I'll do some testing to see what fits better
thanks a lot