> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hexclave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ApiKey

> Reference for the ApiKey type for programmatic backend access.

`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`](#apikey)
- Types:
  * [`UserApiKey`](#userapikey)
  * [`TeamApiKey`](#teamapikey)***# `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()`](https://docs.hexclave.com/sdk/types/user#currentusercreateapikeyoptions)
- [`user.listApiKeys()`](https://docs.hexclave.com/sdk/types/user#currentuserlistapikeys)
- [`user.useApiKeys()`](https://docs.hexclave.com/sdk/types/user#currentuseruseapikeys) (React hook)
- [`team.createApiKey()`](https://docs.hexclave.com/sdk/types/team#teamcreateapikeyoptions)
- [`team.listApiKeys()`](https://docs.hexclave.com/sdk/types/team#teamlistapikeys)
- [`team.useApiKeys()`](https://docs.hexclave.com/sdk/types/team#teamuseapikeys) (React hook)## Table of ContentsApiKey Table of Contents<pre />## Properties

\<details>

\<summary>`apiKey.id`
          
            
            apiKey</summary>

      

The unique identifier for this API key.

Type Definition

\`typescript declare const id: string; `

\</details>



\<details>

\<summary>`apiKey.description`
          
            
            apiKey</summary>

      

A human-readable description of the API key's purpose.

Type Definition

\`typescript declare const description: string; `

\</details>



\<details>

\<summary>`apiKey.expiresAt`
          
            
            apiKey</summary>

      

The date and time when this API key will expire. If not set, the key does not expire.

Type Definition

\`typescript declare const expiresAt: Date | undefined; `

\</details>



\<details>

\<summary>`apiKey.manuallyRevokedAt`
          
            
            apiKey</summary>

      

The date and time when this API key was manually revoked. If `null`, the key has not been revoked. May be `undefined` if the information is not available.

Type Definition

\`typescript declare const manuallyRevokedAt: Date | null | undefined; `

\</details>



\<details>

\<summary>`apiKey.createdAt`
          
            
            apiKey</summary>

      

The date and time when this API key was created.

Type Definition

\`typescript declare const createdAt: Date; `

\</details>



\<details>

\<summary>`apiKey.value`
          
            
            apiKey</summary>

      

The full API key string on first creation; subsequently returns `{ lastFour: string }` for security.

Type Definition

\`typescript declare const value: string | { lastFour: string }; `

\</details>



\<details>

\<summary>`apiKey.type`
          
            
            apiKey</summary>

      

Whether this is a `"user"` or `"team"` API key.

Type Definition

\`typescript declare const type: "user" | "team"; `

\</details>



\<details>

\<summary>`apiKey.userId`
          
            
            apiKey</summary>

      

For user API keys, the ID of the user that owns this API key.

Type Definition

\`typescript declare const userId: string | undefined; `

\</details>



\<details>

\<summary>`apiKey.teamId`
          
            
            apiKey</summary>

      

For team API keys, the ID of the team that owns this API key.

Type Definition

\`typescript declare const teamId: string | undefined; `

\</details>

## Methods

\<details>

\<summary>`apiKey.isValid`
          
            
            apiKey</summary>

      

Checks if the API key is still valid, meaning it is neither expired nor revoked.

Returns

\`boolean`

Signature

\```typescript theme={null}
declare function isValid(): boolean;
\```

Examples

\```typescript theme={null}
if (apiKey.isValid()) {
  console.log("API key is still valid");
} else {
  console.log("API key is invalid");
}
\```

\</details>



\<details>

\<summary>`apiKey.whyInvalid`
          
            
            apiKey</summary>

      

Returns the reason why the API key is invalid, or `null` if it is valid.

Returns

\`"manually-revoked" | "expired" | null`

Signature

\```typescript theme={null}
declare function whyInvalid(): "manually-revoked" | "expired" | null;
\```

Examples

\```typescript theme={null}
const reason = apiKey.whyInvalid();
if (reason) {
  console.log(`API key is invalid because it was ${reason}`);
} else {
  console.log("API key is valid");
}
\```

\</details>



\<details>

\<summary>`apiKey.revoke`
          
            
            apiKey</summary>

      

Revokes the API key, preventing it from being used for authentication.

Returns

\`Promise<void>`

Signature

\```typescript theme={null}
declare function revoke(): Promise<void>;
\```

Examples

\```typescript theme={null}
await apiKey.revoke();
console.log("API key has been revoked");
\```

\</details>



\<details>

\<summary>`apiKey.update(options)`
          
            
            apiKey</summary>

      

Updates the API key properties.

Parameters

\**options.description** `string`

A new description for the API key.

\**options.expiresAt** `Date | null`

A new expiration date, or `null` to remove the expiration.

\**options.revoked** `boolean`

Set to `true` to revoke the API key.

Returns

\`Promise<void>`

Signature

\```typescript theme={null}
declare function update(options: {
  description?: string;
  expiresAt?: Date | null;
  revoked?: boolean;
}): Promise<void>;
\```

Examples

\```typescript theme={null}
await apiKey.update({
  description: "Updated description",
  expiresAt: new Date(Date.now() + 60 * 24 * 60 * 60 * 1000),
});
\```

\</details>

***# Types### `UserApiKey`A type alias for an API key owned by a user.```typescript theme={null}
type UserApiKey = ApiKey<"user", false>;
```### `UserApiKeyFirstView`A type alias for a newly created user API key, which includes the full key value instead of just the last four characters.```typescript theme={null}
type UserApiKeyFirstView = ApiKey<"user", true>;
```### `TeamApiKey`A type alias for an API key owned by a team.```typescript theme={null}
type TeamApiKey = ApiKey<"team", false>;
```### `TeamApiKeyFirstView`A type alias for a newly created team API key, which includes the full key value instead of just the last four characters.```typescript theme={null}
type TeamApiKeyFirstView = ApiKey<"team", true>;
```***# Creation OptionsWhen creating an API key using [`user.createApiKey()`](https://docs.hexclave.com/sdk/types/user#currentusercreateapikeyoptions) or [`team.createApiKey()`](https://docs.hexclave.com/sdk/types/team#teamcreateapikeyoptions), you need to provide an options object.**description** `string` *required*A human-readable description of the API key's purpose.**expiresAt** `Date | null` *required*The date when the API key will expire. Use `null` for keys that don't expire.**isPublic** `boolean`Whether the API key is public. Defaults to `false`. **Secret API Keys** are
monitored by Hexclave'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.```typescript theme={null}
const user = await hexclaveApp.getUser();

const secretKey = await user.createApiKey({
  description: "Backend integration",
  expiresAt: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000),
  isPublic: false,
});

const publicKey = await user.createApiKey({
  description: "Client-side access",
  expiresAt: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
  isPublic: true,
});
```
