apps/docs/content/docs/orm/prisma-client/deployment/edge/deploy-to-deno-deploy.mdx
With this guide, you can learn how to build and deploy a REST API to Deno Deploy. The application uses Prisma ORM to manage tasks in a Prisma Postgres database.
This guide covers Deno CLI, Deno Deploy, Prisma Client with the Postgres adapter, and Prisma Postgres.
Create a new directory and initialize your Prisma project:
mkdir prisma-deno-deploy
cd prisma-deno-deploy
deno run -A npm:prisma@latest init --db
Enter a name for your project and choose a database region.
This command:
prisma/schema.prisma file for your database models.env file with your DATABASE_URLprisma.config.ts configuration fileCreate a deno.json file with the following configuration:
{
"nodeModulesDir": "auto",
"compilerOptions": {
"lib": ["deno.window"],
"types": ["node"]
},
"imports": {
"@prisma/adapter-pg": "npm:@prisma/adapter-pg@^7.0.0",
"@prisma/client": "npm:@prisma/client@^7.0.0",
"prisma": "npm:prisma@^7.0.0"
},
"tasks": {
"dev": "deno run -A --env=.env --watch index.ts",
"db:generate": "deno run -A --env=.env npm:prisma generate",
"db:push": "deno run -A --env=.env npm:prisma db push",
"db:migrate": "deno run -A --env=.env npm:prisma migrate dev",
"db:studio": "deno run -A --env=.env npm:prisma studio"
}
}
:::note
The nodeModulesDir: "auto" setting is required for Prisma to work correctly with Deno. The compilerOptions ensure TypeScript understands Deno globals and npm packages. The import map allows you to use clean import paths like @prisma/adapter-pg instead of npm:@prisma/adapter-pg.
:::
Install the dependencies:
deno install
deno install --allow-scripts
Edit prisma/schema.prisma to add the Deno runtime and a Task model:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
runtime = "deno"
}
datasource db {
provider = "postgresql"
}
model Task {
id Int @id @default(autoincrement())
title String
description String?
completed Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Apply the schema to your database and generate Prisma Client:
deno task db:push
This command:
Task table in your Prisma Postgres database:::info
If you see TypeScript errors in your IDE after generating, restart the Deno language server (Cmd/Ctrl + Shift + P → "Deno: Restart Language Server") to refresh the types.
:::
Create index.ts with a REST API for managing tasks:
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma/client.ts";
// Initialize Prisma Client with the Postgres adapter
const connectionString = Deno.env.get("DATABASE_URL")!;
const adapter = new PrismaPg({ connectionString });
const prisma = new PrismaClient({ adapter });
// Helper to create JSON responses
function json(data: unknown, status = 200): Response {
return new Response(JSON.stringify(data, null, 2), {
status,
headers: { "Content-Type": "application/json" },
});
}
// Request handler
async function handler(request: Request): Promise<Response> {
const url = new URL(request.url);
const path = url.pathname;
const method = request.method;
try {
// GET /tasks - List all tasks
if (method === "GET" && path === "/tasks") {
const tasks = await prisma.task.findMany({
orderBy: { createdAt: "desc" },
});
return json(tasks);
}
// POST /tasks - Create a new task
if (method === "POST" && path === "/tasks") {
const body = await request.json();
const task = await prisma.task.create({
data: {
title: body.title,
description: body.description,
},
});
return json(task, 201);
}
// GET /tasks/:id - Get a specific task
const taskMatch = path.match(/^\/tasks\/(\d+)$/);
if (taskMatch) {
const id = parseInt(taskMatch[1]);
if (method === "GET") {
const task = await prisma.task.findUnique({ where: { id } });
if (!task) return json({ error: "Task not found" }, 404);
return json(task);
}
// PATCH /tasks/:id - Update a task
if (method === "PATCH") {
const body = await request.json();
const task = await prisma.task.update({
where: { id },
data: body,
});
return json(task);
}
// DELETE /tasks/:id - Delete a task
if (method === "DELETE") {
await prisma.task.delete({ where: { id } });
return json({ message: "Task deleted" });
}
}
// GET / - API info
if (method === "GET" && path === "/") {
return json({
name: "Prisma + Deno Task API",
version: "1.0.0",
endpoints: {
"GET /tasks": "List all tasks",
"POST /tasks": "Create a task",
"GET /tasks/:id": "Get a task",
"PATCH /tasks/:id": "Update a task",
"DELETE /tasks/:id": "Delete a task",
},
});
}
return json({ error: "Not found" }, 404);
} catch (error) {
console.error(error);
return json({ error: "Internal server error" }, 500);
}
}
// Start the server
Deno.serve({ port: 8000 }, handler);
This creates a full CRUD API with the following endpoints:
| Method | Endpoint | Description |
|---|---|---|
| GET | / | API info |
| GET | /tasks | List all tasks |
| POST | /tasks | Create a new task |
| GET | /tasks/:id | Get a specific task |
| PATCH | /tasks/:id | Update a task |
| DELETE | /tasks/:id | Delete a task |
Start the development server:
deno task dev
Test the API with curl:
# Get API info
curl http://localhost:8000/
# Create a task
curl -X POST http://localhost:8000/tasks \
-H "Content-Type: application/json" \
-d '{"title": "Learn Prisma", "description": "Complete the Deno guide"}'
# List all tasks
curl http://localhost:8000/tasks
# Update a task (mark as completed)
curl -X PATCH http://localhost:8000/tasks/1 \
-H "Content-Type: application/json" \
-d '{"completed": true}'
# Delete a task
curl -X DELETE http://localhost:8000/tasks/1
You should see JSON responses for each request. The task ID will increment with each new task created.
You need a GitHub repository to deploy to Deno Deploy.
Create a .gitignore file:
.env
node_modules/
generated/
deno.lock
Initialize and push your repository:
git init -b main
git remote add origin https://github.com/<username>/prisma-deno-deploy
git add .
git commit -m "Initial commit"
git push -u origin main
deno installdeno run -A npm:prisma generateindex.tsThe first deployment will fail because you need to add the database connection string.
DATABASE_URL.env file)Trigger a new deployment by clicking Redeploy or pushing a new commit.
Once deployed, test your API at your Deno Deploy URL:
# Replace with your actual Deno Deploy URL
curl https://your-project.deno.dev/
# Create a task
curl -X POST https://your-project.deno.dev/tasks \
-H "Content-Type: application/json" \
-d '{"title": "Deploy to production"}'
# List tasks
curl https://your-project.deno.dev/tasks
You successfully deployed a REST API to Deno Deploy using:
Your project structure should look like this:
prisma-deno-deploy/
├── deno.json
├── index.ts
├── prisma/
│ └── schema.prisma
├── prisma.config.ts
├── generated/
│ └── prisma/
│ └── ...
└── .env