Skip to content
Back

Function not creating documents

  • 0
  • Self Hosted
  • Functions
f4ls3
18 Aug, 2025, 19:00

So Im having this issue where no error is thrown at all but my function isn't able to create documents inside my database.

TypeScript
import { Client, Databases, ID } from "node-appwrite";

export default async ({ req, res, log, error }) => {
  if (req.method !== "POST")
    return res.json({ message: "method not allowed", status: 405 }, 405);

  const client = new Client()
    .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(req.headers["x-api-key"]);

  const databases = new Databases(client);

  const body = req.bodyJson;

  if (!body.level || !body.message || !body.timestamp)
    return res.json({ message: "wrong log format", status: 400 }, 400);

  const level = body.level.toLowerCase();

  if (
    level !== "info" &&
    level !== "error" &&
    level !== "debug" &&
    level !== "warning" &&
    level !== "notice" &&
    level !== "crit" &&
    level !== "alert" &&
    level !== "emerg"
  )
    return res.json({ message: "invalid log level", status: 400 }, 400);

  try {
    await databases.createDocument(
      process.env.DATABASE_ID,
      process.env.COLLECTION_ID,
      ID.unique(),
      body
    )
      .then((result) => log(result.$id))
      .catch((err) => error(err));
  } catch (e) {
    error(e);
    return res.json({ message: "no access", status: 403 }, 403);
  }

  return res.empty();
};

Above is the function. I've given the key inside the header all permissions that I could. As I said there are no errors thrown or anything to indicate that the creation has failed but its not creating the document

TL;DR
Function is not creating documents in the database despite no error being thrown. The issue may be related to improper permissions or incorrect configurations. Ensure that the key provided in the header has all necessary permissions. Check if the database ID and collection ID are correct. Verify the formatting of the log data being sent to the function.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more