Skip to main content

Getting Started

Pikku is a minimal TypeScript framework for building function-based APIs that run on serverless platforms or traditional servers with ease.

Installation & Requirements
Requires Node 18+.
Install via npm install @pikku/core or yarn add @pikku/core.

This guide walks you through the setup steps. For more complete examples, see our repos on GitHub.

Step One

Types

Define four key types to get started: Config, UserSession,SingletonServices, and SessionServices.

import {
    CoreConfig,
    CoreServices,
    CoreSingletonServices,
    CoreUserSession,
  } from '@pikku/core'
  
  // This contains all the configuration for the application, created once on start.
  export interface Config extends CoreConfig {}
  
  // This holds the user-session data, retrieved via the HTTPSessionService on each request.
  export interface UserSession extends CoreUserSession {}
  
  // Singleton services, created once on start, shared across all requests.
  export type SingletonServices = CoreSingletonServices & {
    config: Config,
    jwt: JWTService<UserSession>
  }
  
  // Wire services, created per HTTP request, queue, CLI command,
  // or entire websocket lifetime (when local).
  export interface Services extends CoreServices<SingletonServices> {}
  

Step Two

Type Implementations

Next, create the implementations of these types that the Pikku CLI uses.

import { pikkuConfig, pikkuServices, pikkuWireServices } from '#pikku/pikku-types.gen.js'

  /**
   * Loads configuration for the application (created once at startup).
   */
  export const createConfig = pikkuConfig(async () => {
    return {
      logLevel: LogLevel.info
    }
  })

  /**
   * Creates the singleton services for the application (created once at startup).
   */
  export const createSingletonServices = pikkuServices(
    async (config, existingServices) => {
      const variablesService = new LocalVariablesService()
      const logger = new ConsoleLogger()
      const jwt = new JoseJWTService<UserSession>(keys, logger)
      const httpSessionService = new PikkuHTTPSessionService<UserSession>(jwt, {})

      return {
        config,
        logger,
        variablesService,
        jwt,
        httpSessionService
      }
    }
  )

  /**
   * Creates the wire services per HTTP request, queue, CLI command,
   * or entire websocket lifetime (when local).
   */
  export const createWireServices = pikkuWireServices(
    async (_services, _wire) => {
      return {}
    }
  )
  

Step Three

Logic Functions

With dependencies in place, define your logic functions. For instance:

Step Four

Pikku CLI

pikku.config.json instructs the CLI where to find routes and generate additional types:

{
    "$schema": "https://raw.githubusercontent.com/pikkujs/pikku/refs/heads/main/packages/cli/cli.schema.json",
    "tsconfig": "./tsconfig.json",
    "srcDirectories": ["src"],
    "outDir": ".pikku"
  }
  
Generate Necessary Files
Then run npx @pikku/cli to generate all the necessary files.

Step Five

Integration and Deployment

Pikku can integrate with various platforms:

WebSocket Examples
Examples with WebSockets are not yet included in this section.

Required: @pikku/express-middleware

import express from 'express'
  import { pikkuExpressMiddleware } from '@pikku/express-middleware'
  import { createWireServices } from '../src/services.js'

  import '../.pikku/pikku-bootstrap'

  const start = async () => {
    const app = express()
    const port = 3000

    const config = await createConfig()
    const singletonServices = await createSingletonServices(config)

    app.use(
      pikkuExpressMiddleware(singletonServices, createWireServices, {
        respondWith404: false,
      })
    )
  
    app.listen(port, () => {
      console.log(`Example app listening on port ${port}`)
    })
  }
  
  start()
  
Express Deployment
This app starts an Express server on port 3000. The pikku middleware processes routes.

Next Steps

Where to Go from Here

You now have a working understanding of Pikku. Explore advanced topics like authentication, real-time messaging, or connect with the community on GitHub!

Happy Coding!
We look forward to your feedback and contributions.