Skip to content
Back

[SOLVED] Function not creating documents

  • 0
  • Resolved
  • 3
  • 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 was not creating documents, tried various solutions without success. After debugging, it was found that the `APPWRITE_FUNCTION_API_ENDPOINT` was set with HTTP instead of HTTPS causing the issue. By changing the value to HTTPS in the `.env` file, the problem was resolved.
18 Aug, 2025, 20:09

What are you expecting to be in x-api-key, I don't think that's a supported header?...

18 Aug, 2025, 20:10

Are you wanting the dynamic api key? If so thats x-appwrite-key

18 Aug, 2025, 20:12

no, im expecting the request to have set that header. this header actually contains an api key. I tested if the key was actually received right in the function and it is. If I change the keys permissions in the console to have no database access it throws an error for insufficient permissions but if i give it access to database it shows no error. this leads me to think that it isnt a problem with the key

18 Aug, 2025, 20:17

Probably not the issue but that try catch wouldn't actually make it to the catch block since you're catching it in your then.catch statement

18 Aug, 2025, 20:17

What is the output of the createDocument? Nothing?

18 Aug, 2025, 20:17

yes, i tired catching both ways with and without handling the promise. both no results

18 Aug, 2025, 20:17

exactly, literally nothing

18 Aug, 2025, 20:18

when printing the doc.$id it outputs undefined

18 Aug, 2025, 20:19

Can you do this

TypeScript
try {
  const res = await databases.createDocument(
    process.env.DATABASE_ID,
    process.env.COLLECTION_ID,
    ID.unique(),
    body
  );

  console.log(res);

} catch (e) {
  console.log(e);

  return res.text("error");
}
18 Aug, 2025, 20:21
18 Aug, 2025, 20:22
18 Aug, 2025, 20:22

this document that is logged i added manually

18 Aug, 2025, 20:22
18 Aug, 2025, 20:22

this was the request

18 Aug, 2025, 20:23

oh and the request returned with 204

18 Aug, 2025, 20:26

yea 204 is no content, which is expected if it worked

18 Aug, 2025, 20:26

so is your function listing documents too?

18 Aug, 2025, 20:26

nope not supposed to

18 Aug, 2025, 20:27

also didnt add anything to the db

18 Aug, 2025, 20:27

is that data coming from the console.log after the createDocument function?

18 Aug, 2025, 20:27

the console.log(res) must print the db's content then

18 Aug, 2025, 20:27

thats what i thought too

18 Aug, 2025, 20:27

wild that isn't right?

18 Aug, 2025, 20:28

what version of node-appwrite

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