# Backends & REST APIs

## Example generated code

src/index.ts:

```ts
import { Hono } from "hono";

const app = new Hono();

// Serve the application's frontend.
app.get("/", (c) => {
	return c.html(`<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Hello from Dynamic Apps</title>
	</head>
	<body>
		<main>
			<h1>Hello from Dynamic Apps</h1>
			<p>This HTML is served by an HTTP app running inside agentOS.</p>
			<p><a href="./api/hello">Call the JSON API</a></p>
		</main>
	</body>
</html>`);
});

// Serve a REST API request from the same application.
app.get("/api/hello", (c) => {
	return c.json({ message: "Hello from Dynamic Apps" });
});

export default app;
```

package.json:

```json
{
	"name": "hello-world-app",
	"version": "0.0.0",
	"private": true,
	"main": "src/index.ts",
	"dependencies": {
		"hono": "4.13.5"
	}
}
```

## Deploy and route

Deploy the directory and every route the app defines is served under
`/apps/:appId` on your routing server:

src/server.ts:

```ts
import { serve } from "@hono/node-server";
import { appsRouter } from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();

// This is how the Rivet control plane communicates with your backend.
server.all("/api/rivet/*", (c) => appsRouter.fetch(c.req.raw));

// Mount every deployed application at /apps/:appId.
server.route("/apps", appsRouter);

// Serve the host router over HTTP.
serve({
	fetch: server.fetch,
	port: 3000,
});
```

See [Route](/dynamic-apps/docs/routing) for how requests reach the app and
[Deploy](/dynamic-apps/docs/deploy) for builds and releases.
