What Are API Functions? Core Methods, Handlers, and Endpoint Actions Explained

What are API functions? Discover how server controller functions, client SDK wrappers, and HTTP handlers work together to power software integrations.

When developers speak about api functions, they typically refer to one of two things: the backend controller functions that handle incoming HTTP routes, or the client-side software development kit (SDK) functions that wrap network requests in ergonomic syntax.

1. Server-Side API Functions (Route Handlers)

On the backend, an api function is the executable routine bound to a route. In Express, Next.js, or FastAPI, it accepts a request object, performs business logic, and returns a response:

// Server-side API Function in TypeScript
export async function handleCreateInvoice(req: Request, res: Response) {
  try {
    const { customerId, items, currency } = req.body;
    
    // 1. Validation function
    validateInvoicePayload({ customerId, items });

    // 2. Database business logic
    const invoice = await db.invoices.create({
      data: { customerId, items, currency, status: 'unpaid' }
    });

    // 3. Return response function
    return res.status(201).json({ success: true, invoice });
  } catch (err) {
    return res.status(400).json({ error: err.message });
  }
}

2. Client-Side API Functions (SDK Wrappers)

On the frontend, calling raw fetch() repeatedly is error-prone. Developers encapsulate network calls into reusable api functions:

// Client API function SDK
export const api = {
  getProfile: (userId: string) => 
    fetch(`/api/users/${userId}`).then(r => r.json()),
    
  updateAvatar: (userId: string, file: File) => {
    const form = new FormData();
    form.append('avatar', file);
    return fetch(`/api/users/${userId}/avatar`, { method: 'POST', body: form });
  }
};

Well-organized api functions keep frontend code clean, reduce boilerplate, and centralize error handling and authentication token refreshes in one place.