API Calls

Making API calls is as simple as making function calls

Calling API endpoints between services, i.e. service-to-service calls, looks like regular function calls with Encore.ts. This gives you a simple monolith-like developer experience, even when you use multiple services. The only thing you need to do is import the service you want to call from ~encore/clients and then call its API endpoints like functions.

This works because, when compiling your application, Encore uses static analysis to parse all APIs and make them available through the ~encore/clients module for internal calls. You get all the benefits of function calls, like compile-time checking of all the parameters and auto-completion in your editor, while still allowing the division of code into logical components, services, and systems.

Example

In the example below, we import the service hello and call the ping endpoint using a function call to hello.ping.

import { hello } from "~encore/clients"; // import 'hello' service export const myOtherAPI = api({}, async (): Promise<void> => { const resp = await hello.ping({ name: "World" }); console.log(resp.message); // "Hello World!" });
Related example
Simple microservices example application with service-to-service API calls.
$ encore app create --example=ts/simple-event-driven