Encore JavaScript/TypeScript SDK
Encore's JavaScript/TypeScript SDK, currently at stable version 1.56.6, provides the necessary APIs and development utilities for building backend applications within the Encore cloud platform. It enables developers to define services, interact with built-in platform features like caching and secrets, and manage application infrastructure using a declarative, type-safe approach. The SDK integrates seamlessly with the Encore CLI and platform, abstracting away complex infrastructure provisioning. Recent updates include comprehensive built-in support for caching in Encore.ts, powered by Redis, offering various type-safe keyspace types such as `StringKeyspace` and `StructKeyspace`. It also includes internal improvements for Go 1.26 support and enhanced secret management features via the CLI. Releases are frequent, typically focusing on minor fixes, performance enhancements, and new platform feature integrations.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an ES Module (ESM) context, which is the default for modern TypeScript/JavaScript projects using `encore.dev`.fixUse ES module `import` statements instead: `import { someSymbol } from 'encore.dev';` -
Error: Encore services must be run within the Encore development environment or deployed.
cause Attempting to execute an Encore service file directly with `node` or `ts-node` without the Encore runtime context.fixRun your Encore application using the `encore run` command from your terminal, or deploy it with `encore deploy`. -
TypeError: Cannot read properties of undefined (reading 'get') or 'api' is not a function
cause This usually indicates that the `encore.dev` module was not correctly imported, or the Encore runtime environment is not active, leading to undefined SDK objects.fixVerify that `import { api, Cache } from 'encore.dev';` is at the top of your file and ensure your application is launched via `encore run` or deployed to the Encore platform.
Warnings
- gotcha The SDK requires Node.js version 18.0.0 or higher. Running with older versions may lead to unexpected errors or incompatible syntax.
- gotcha This package is an SDK for the Encore platform and is not a standalone library. It requires the Encore CLI and development environment or a deployed Encore application to function correctly.
- deprecated The `encore secret archive` and `encore secret unarchive` CLI commands have been deprecated in favor of `encore secret delete`. While this primarily affects the CLI, it impacts the overall secret management workflow.
Install
-
npm install encore.dev -
yarn add encore.dev -
pnpm add encore.dev
Imports
- api
const { api } = require('encore.dev');import { api } from 'encore.dev'; - Cache
import Cache from 'encore.dev';
import { Cache } from 'encore.dev'; - Auth
const { Auth } = require('encore.dev');import { Auth } from 'encore.dev';
Quickstart
import { api, Cache, StringKeyspace } from 'encore.dev';
// Define a type for user data that will be cached
interface UserData {
id: string;
name: string;
email: string;
}
// Declare a type-safe cache keyspace for user data
const userCache = Cache.declare(
'user_data',
StringKeyspace<UserData>() // Keys are strings, values are UserData objects
);
// Define an Encore service named 'UserService'
// This service exposes a GET /user/:id API endpoint
export const user = api(
{
name: 'UserService',
endpoint: 'user',
path: '/user/:id',
method: 'GET',
},
async (id: string): Promise<UserData> => {
// Try to retrieve user data from the cache first
const cachedUser = await userCache.get(id);
if (cachedUser) {
console.log(`[Encore] User ${id} found in cache.`);
return cachedUser;
}
// If not in cache, simulate fetching from a 'database'
console.log(`[Encore] Fetching user ${id} from database...`);
await new Promise(resolve => setTimeout(resolve, 200)); // Simulate async DB call
const userData: UserData = {
id: id,
name: `User ${id} Name`,
email: `user${id}@example.com`
};
// Store the fetched data in the cache for 5 minutes (300 seconds)
await userCache.put(id, userData, { ttl: 300 });
return userData;
}
);
// To run this, save as user.ts in an Encore application directory
// and execute 'encore run'. The API endpoint will be exposed automatically.