# API Key

> API key with scopes, creation date, expiration, and last-used tracking.

- Name: `api-key`
- Categories: api
- Detail page: https://open-types.dev/types/api-key

## Install

```bash
# Types only
npx shadcn add @open-types/api-key

# Types + Zod v4 validators
npx shadcn add @open-types/api-key-zod

# Types + real-world examples
npx shadcn add @open-types/api-key-examples
```

## Types

```typescript
export interface APIKey {
  id: string;
  key: string;
  name?: string;
  scopes?: string[];
  created_at: string;
  expires_at?: string;
  last_used_at?: string;
}
```

## Zod validator

```typescript
import * as z from "zod";
import type { APIKey } from "../types/api-key";

export const apiKeySchema = z.object({
  id: z.string().min(1, { error: "API key ID is required" }),
  key: z.string().min(1, { error: "API key value is required" }),
  name: z.string().optional(),
  scopes: z.array(z.string().min(1, { error: "Scope cannot be empty" })).optional(),
  created_at: z.iso.datetime({ error: "Invalid created_at timestamp" }),
  expires_at: z.iso.datetime({ error: "Invalid expires_at timestamp" }).optional(),
  last_used_at: z.iso.datetime({ error: "Invalid last_used_at timestamp" }).optional(),
}) satisfies z.ZodType<APIKey>;

export type { APIKey } from "../types/api-key";
```

## Examples

```typescript
// Source: hand-crafted
import type { APIKey } from "../types/api-key";

export const apiKeyExamples = {
  full: {
    id: "key_01HABCDEF0123456789ABCDE",
    key: "sk_live_abcdefghijklmnopqrstuvwxyz0123456789",
    name: "Production server",
    scopes: ["read", "write"],
    created_at: "2026-04-13T18:30:00Z",
    expires_at: "2027-04-13T18:30:00Z",
    last_used_at: "2026-05-16T09:15:00Z",
  } satisfies APIKey,
  minimal: {
    id: "key_minimal_001",
    key: "sk_test_minimal_token_value",
    created_at: "2026-05-16T18:30:00Z",
  } satisfies APIKey,
  readOnlyExpired: {
    id: "key_readonly_002",
    key: "sk_test_readonly_token",
    name: "Reporting bot",
    scopes: ["read"],
    created_at: "2025-01-01T00:00:00Z",
    expires_at: "2025-12-31T23:59:59Z",
    last_used_at: "2025-12-31T18:30:00Z",
  } satisfies APIKey,
  neverUsed: {
    id: "key_unused_003",
    key: "sk_test_unused_token",
    name: "Standby key",
    scopes: ["admin"],
    created_at: "2026-05-16T18:30:00Z",
  } satisfies APIKey,
  multiScope: {
    id: "key_multi_004",
    key: "sk_live_multi_scope_token",
    name: "CI pipeline",
    scopes: ["read", "write", "deploy:create", "audit:read"],
    created_at: "2026-01-01T00:00:00Z",
    last_used_at: "2026-05-16T18:30:00Z",
  } satisfies APIKey,
} as const;
```
