#If I use @convex-dev/r2 and use r2.generateUrl() in a query for multiple files, each call counts?

4 messages · Page 1 of 1 (latest)

mystic sinew
#

If I use @convex-dev/r2, and in a query, I call multiple entries, I need to generate a url for each entry using r2.generateUrl(entry.key), does each url generated count as a query in my billing or not?

jolly thistleBOT
#

Hi @mystic sinew. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the available knowledge sources, when you use @convex-dev/r2 and call r2.getUrl (or r2.generateUrl) within a single query function to generate URLs for multiple files, all of those URL generations happen within the context of that single query function call. In Convex's billing model, you are billed per function call (i.e., each invocation of a query, mutation, or action), not per internal operation within that function.

So, if you have a query that fetches multiple entries and calls r2.getUrl for each entry, this still counts as a single function call for billing purposes, regardless of how many URLs you generate inside that query. The number of internal r2.getUrl calls does not multiply your function call count.

Here’s an example from the docs, where multiple URLs are generated in a single query:

// convex/listMessages.ts
import { components } from "./_generated/api";
import { query } from "./_generated/server";
import { R2 } from "@convex-dev/r2";

const r2 = new R2(components.r2);

export const list = query({
  args: {},
  handler: async (ctx) => {
    // In this example, messages have an imageKey field with the object key
    const messages = await ctx.db.query("messages").collect();
    return Promise.all(
      messages.map(async (message) => ({
        ...message,
        imageUrl: await r2.getUrl(message.imageKey),
      }))
    );
  },
});

This query generates a URL for each message, but it is still a single query function call from the perspective of Convex billing Components > Cloudflare R2: Serving Files.

#

Summary:
You are billed for the number of query/mutation/action function calls, not for each internal call to r2.getUrl or similar helpers within a single function. So, generating multiple URLs in a single query does not increase your function call count.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: