API Key
slug: sdk/types/api-key
ApiKey
represents an authentication token that allows programmatic access to your application’s backend. API keys can be associated with individual users or teams.
On this page:
ApiKey
- Types:
ApiKey
API keys provide a way for users to authenticate with your backend services without using their primary credentials. They can be created for individual users or for teams, allowing programmatic access to your application.
API keys can be obtained through:
user.createApiKey()
user.listApiKeys()
user.useApiKeys()
(React hook)team.createApiKey()
team.listApiKeys()
team.useApiKeys()
(React hook)
Table of Contents
apiKey.id
The unique identifier for this API key.
Type Definition
apiKey.description
A human-readable description of the API key’s purpose.
Type Definition
apiKey.expiresAt
The date and time when this API key will expire. If not set, the key does not expire.
Type Definition
apiKey.manuallyRevokedAt
The date and time when this API key was manually revoked. If null, the key has not been revoked.
Type Definition
apiKey.createdAt
The date and time when this API key was created.
Type Definition
apiKey.value
The value of the API key. When the key is first created, this is the full API key string. After that, only the last four characters are available for security reasons.
Type Definition
apiKey.userId
For user API keys, the ID of the user that owns this API key.
Type Definition
apiKey.teamId
For team API keys, the ID of the team that owns this API key.
Type Definition
apiKey.isValid()
Checks if the API key is still valid (not expired and not revoked).
Parameters
None.
Returns
boolean
: True if the key is valid, false otherwise.
Signature
Examples
apiKey.whyInvalid()
Returns the reason why the API key is invalid, or null if it is valid.
Parameters
None.
Returns
"manually-revoked" | "expired" | null
: The reason the key is invalid, or null if it’s valid.
Signature
Examples
apiKey.revoke()
Revokes the API key, preventing it from being used for authentication.
Parameters
None.
Returns
Promise<void>
Signature
Examples
apiKey.update(options)
Updates the API key properties.
Parameters
An object containing properties for updating.
A new description for the API key.
A new expiration date, or null to remove the expiration.
Set to true to revoke the API key.
Returns
Promise<void>
Signature
Examples
Types
UserApiKey
A type alias for an API key owned by a user:
UserApiKeyFirstView
A type alias for a newly created user API key, which includes the full key value:
TeamApiKey
A type alias for an API key owned by a team:
TeamApiKeyFirstView
A type alias for a newly created team API key, which includes the full key value:
Creation options
When creating an API key using user.createApiKey()
or team.createApiKey()
, you need to provide an options object with the following properties:
Properties
A human-readable description of the API key’s purpose.
The date when the API key will expire. Use null for keys that don’t expire.
Whether the API key is public. Defaults to false.
- Secret API Keys (default) are monitored by Stack Auth’s secret scanner, which can revoke them if detected in public code repositories.
- Public API Keys are designed for client-side code where exposure is not a concern.