Putnami
DocsGitHub

Licensed under FSL-1.1-MIT

Getting Started
Concepts
How To
Build A Web App
Build An Api Service
Share Code Between Projects
Configure Your App
Add Persistence
Add Authentication
Add Background Jobs
Develop With Ai
Structure Business Logic With Di
Upgrade Putnami
Principles
Tooling & Workspace
Workspace
Cli
Jobs & Caching
Extensions
Templates
Error Handling
Frameworks
Typescript
ExtensionOverviewWebReact RoutingForms And ActionsStatic FilesApiErrors And ResponsesConfigurationLoggingHttp And MiddlewareDependency InjectionPlugins And LifecycleSessionsAuthPersistenceEventsStorageCachingWebsocketsTestingHealth ChecksTelemetryProto GrpcSmart ClientSchema
Go
ExtensionOverviewHttpDependency InjectionPlugins And LifecycleConfigurationSecurityPersistenceErrorsEventsStorageCachingLoggingTelemetryGrpcService ClientsValidationOpenapiTesting
Python
Extension
Platform
Ci
  1. DocsSeparator
  2. How ToSeparator
  3. Share Code Between Projects

Share code between projects

You will create a shared library, export a service from it, and use it from an app via dependency injection.

Steps

1) Create a shared package

putnami projects create @workspace/shared --template typescript-library

This scaffolds a TypeScript library at packages/workspace/shared/ with exports and a test suite.

2) Export a shared service

Replace packages/workspace/shared/src/index.ts:

export class GreetingService {
  message() {
    return 'Hello from shared code';
  }
}

No decorators or runtime imports needed — services are plain classes.

3) Use it in an app

In your app package.json, add the shared package as a dependency:

"@workspace/shared": "workspace:*"

Register the service in your application entry point (src/main.ts):

import { application, api, http } from '@putnami/application';
import { GreetingService } from '@workspace/shared';

export const app = () =>
  application()
    .use(http())
    .provide(GreetingService)
    .use(api());

Then inject it in a route, for example src/api/hello/get.ts:

import { endpoint } from '@putnami/application';
import { GreetingService } from '@workspace/shared';

export const GET = endpoint()
  .inject({ greeting: GreetingService })
  .handle(({ greeting }) => ({ message: greeting.message() }));

Result

You now have shared code used by more than one project. The library is built, tested, and linked through workspace dependencies.

Companion sample: typescript/samples/05-dependency-injection — a runnable project with explicit provider registration, endpoint().inject(), and scoped services. Run putnami serve @example/dependency-injection from the workspace root.

On this page

  • Share code between projects
  • Steps
  • 1) Create a shared package
  • 2) Export a shared service
  • 3) Use it in an app
  • Result