Integrating a Python endpoint in the t3 stack
Is it possible to integrate a Python endpoint in the t3 stack? I need a Python endpoint because the package that I wanna use is an AI one and currently unavailable in JS/TS.
/api and it might cause a few issues with 3rd party packages that are expecting pages/api to exist./pages/api folder in the project I was gonna test since I have some third party packages I didn't want to mess with. import type { ChildProcessWithoutNullStreams } from "child_process";
import { spawn } from "child_process";
import { trycatch } from "../../../utils/trycatch";
import { createTRPCRouter, protectedProcedure } from "../trpc";
export const pythonRouter = createTRPCRouter({
test: protectedProcedure.query(async () => {
return await trycatch({
fn: () => {
const python: ChildProcessWithoutNullStreams = spawn("python3", [
"src/server/api/python/testing.py",
]);
return new Promise<string>((resolve, reject) => {
let output = "";
python.stdout.on("data", (data: Buffer) => {
output += data.toString();
});
python.stderr.on("data", (data: Buffer) => {
reject(data.toString());
});
python.on("close", (code) => {
if (code && code !== 0) {
reject(`Process exited with code ${code}`);
} else {
resolve(output);
}
});
});
},
errorMessages: ["Failed to run python code"],
})();
}),
});In short, if you're using another language to write a serverless function inside a Next.js project. Be sure to place it inside an api folder that sits in the root directory of the project (if there's none, create one).pages/api/pages/api