Hello @Atin
Thanks for using Q & A forum.
Follow below steps:
- Create Typescript Http Triggered Azure function.
Run the commands:
npm install @prisma --save-dev
npm install @prisma/client
npx prisma init
.env_ and the schema.prisma file gets created.
schema.prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model todo {
completed Boolean? @default(false)
id Int @default(autoincrement()) @id
todo String
}
- Code snippet:
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
interface TodoRequestBody {
todo: string;
}
export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`HTTP function processed request for URL "${request.url}"`);
try {
const body = await request.json() as TodoRequestBody;
if (body && body.todo) {
const todo = body.todo;
const item = await prisma.todo.create({
data: { todo }
});
return {
status: 200,
jsonBody: item
};
} else {
return {
status: 400,
body: "Please pass a todo in the request body"
};
}
} catch (error) {
context.log(error);
return {
status: 500,
body: "An error occurred on the server"
};
}
}
app.http('httpTrigger1', {
methods: ['POST'],
authLevel: 'anonymous',
handler: httpTrigger1
});
- Provide the correct connection string of postgres database in .env file:
DATABASE_URL="postgresql://username:password@hostname:port/database?schema=public"
- Run npx prisma generate to generate Prisma client libraries in a separate folder named Prisma.
Able to run locally:
Functions:
httpTrigger: [GET,POST] http://localhost:7071/api/httpTrigger
For detailed output, run func with --verbose flag.
[2025-03-03T07:18:26.898Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2025-03-03T07:18:59.023Z] Executing 'Functions.httpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=48eb753d-52a1-42d9-b925-6575546a06f6)
[2025-03-03T07:18:59.150Z] Http function processed request for url "http://localhost:7071/api/httpTrigger"
[2025-03-03T07:18:59.248Z] Executed 'Functions.httpTrigger' (Succeeded, Id=48eb753d-52a1-42d9-b925-6575546a06f6, Duration=263ms)
Add DATABASE_URL=<Postgres_connection_string>
in the Function App's App Settings.
Deployed to Azure:
C:\Users\uname\nodefn>npx func azure functionapp publish kpfn3
Getting site publishing info...
[2025-03-03T07:20:53.062Z] Starting the function app deployment...
Creating archive for current directory...
Uploading 80.91 MB [##############################################################################]
Upload completed successfully.
Deployment completed successfully.
[2025-03-03T07:22:28.282Z] Syncing triggers...
Functions in kpfn3:
httpTrigger - [httpTrigger]
Invoke url: https://function.azurewebsites.net/api/httptrigger
Portal:
2025-03-03T07:24:28Z [Verbose] AuthenticationScheme: WebJobsAuthLevel was successfully authenticated.
2025-03-03T07:24:28Z [Verbose] Authorization was successful.
2025-03-03T07:24:28Z [Information] Executing 'Functions.httpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=171c936f-08af-4bdd-bc94-9235c8a194e8)
2025-03-03T07:24:28Z [Verbose] Sending invocation id: '171c936f-08af-4bdd-bc94-9235c8a194e8
2025-03-03T07:24:28Z [Verbose] Posting invocation id:171c936f-08af-4bdd-bc94-9235c8a194e8 on workerId:edd86f41-6037-4de3-b4af-7945f3354afe
2025-03-03T07:24:29Z [Information] Http function processed request for url "https://function.azurewebsites.net/api/httpTrigger"
2025-03-03T07:24:29Z [Information] Executed 'Functions.httpTrigger' (Succeeded, Id=171c936f-08af-4bdd-bc94-9235c8a194e8, Duration=171ms)
Hope this helps.
If the Answer is helpful, please click Accept Answer
and Up-Vote, so that it can help others in the community looking for help on similar topics.