Primitives.org.ai

API

Define programmatic interfaces

API

The API() function defines programmatic interfaces with endpoints, authentication, and rate limiting.

Basic Usage

import { API, Endpoint } from 'digital-products'

const myAPI = API({
  id: 'my-api',
  name: 'My API',
  description: 'A RESTful API',
  version: '1.0.0',
  style: 'rest',
  baseUrl: 'https://api.example.com',
})

Endpoints

Define API endpoints:

import { API, Endpoint } from 'digital-products'

const myAPI = API({
  id: 'my-api',
  name: 'My API',
  version: '1.0.0',
  style: 'rest',
  baseUrl: 'https://api.example.com',
  endpoints: [
    Endpoint('GET', '/users', 'List all users', {
      response: {
        users: ['Array of user objects'],
        total: 'Total count (number)',
      },
    }),
    Endpoint('GET', '/users/:id', 'Get user by ID', {
      response: {
        id: 'User ID',
        name: 'User name',
        email: 'User email',
      },
    }),
    Endpoint('POST', '/users', 'Create a user', {
      request: {
        name: 'User name',
        email: 'User email',
      },
      response: {
        id: 'User ID',
        name: 'User name',
        email: 'User email',
      },
      auth: true,
    }),
    Endpoint('PUT', '/users/:id', 'Update user', {
      request: {
        name: 'User name',
        email: 'User email',
      },
      auth: true,
    }),
    Endpoint('DELETE', '/users/:id', 'Delete user', {
      auth: true,
    }),
  ],
})

Endpoint Helper

Endpoint(method, path, description, options?)
ParameterTypeDescription
methodstringHTTP method
pathstringURL path
descriptionstringEndpoint description
options.requestschemaRequest body schema
options.responseschemaResponse schema
options.authbooleanRequires auth

API Styles

StyleDescription
restRESTful API
graphqlGraphQL API
rpcRemote procedure call
grpcgRPC API
websocketWebSocket API

Authentication

Add API authentication:

import { API, APIAuth } from 'digital-products'

const myAPI = API({
  id: 'my-api',
  name: 'My API',
  version: '1.0.0',
  style: 'rest',
  auth: APIAuth({
    type: 'bearer',   // or 'api-key', 'oauth2', 'basic'
    header: 'Authorization',
  }),
})

Auth Types

TypeDescription
bearerBearer token in header
api-keyAPI key authentication
oauth2OAuth 2.0 flow
basicBasic authentication

Rate Limiting

Configure rate limits:

import { API, RateLimit } from 'digital-products'

const myAPI = API({
  id: 'my-api',
  name: 'My API',
  version: '1.0.0',
  style: 'rest',
  rateLimit: RateLimit({
    requests: 100,      // Max requests
    window: 60,         // Per 60 seconds
    onExceeded: 'reject', // or 'queue'
  }),
})

Complete Example

import { API, Endpoint, APIAuth, RateLimit } from 'digital-products'

const ordersAPI = API({
  id: 'orders-api',
  name: 'Orders API',
  description: 'E-commerce orders management API',
  version: '2.0.0',
  style: 'rest',
  baseUrl: 'https://api.store.com/v2',

  endpoints: [
    Endpoint('GET', '/orders', 'List orders', {
      response: {
        orders: [{
          id: 'Order ID',
          status: 'pending | processing | shipped | delivered',
          total: 'Order total (number)',
          items: ['Array of order items'],
        }],
        pagination: {
          page: 'Current page (number)',
          totalPages: 'Total pages (number)',
        },
      },
    }),
    Endpoint('POST', '/orders', 'Create order', {
      request: {
        items: [{
          productId: 'Product ID',
          quantity: 'Quantity (number)',
        }],
        shippingAddress: {
          street: 'Street address',
          city: 'City',
          zip: 'ZIP code',
        },
      },
      response: {
        id: 'Order ID',
        status: 'Order status',
      },
      auth: true,
    }),
    Endpoint('GET', '/orders/:id', 'Get order details', {
      response: {
        id: 'Order ID',
        status: 'Order status',
        items: ['Order items'],
        total: 'Total (number)',
        createdAt: 'Created date (date)',
      },
      auth: true,
    }),
  ],

  auth: APIAuth({
    type: 'bearer',
    header: 'Authorization',
  }),

  rateLimit: RateLimit({
    requests: 1000,
    window: 3600,
    onExceeded: 'reject',
  }),
})

OpenAPI/Swagger

APIs automatically generate OpenAPI specs:

// Access the OpenAPI spec
const spec = myAPI.toOpenAPI()
// Returns OpenAPI 3.0 compliant specification

Type Definition

interface APIDefinition {
  id: string
  name: string
  description?: string
  version?: string
  style?: 'rest' | 'graphql' | 'rpc' | 'grpc' | 'websocket'
  baseUrl?: string
  endpoints?: EndpointDefinition[]
  auth?: APIAuthDefinition
  rateLimit?: RateLimitDefinition
  metadata?: Record<string, unknown>
}
Was this page helpful?

On this page