#How to call convex server from convex client?

4 messages · Page 1 of 1 (latest)

dusk bay
#

my convex server:
convex/connectionStorage.ts
export const set = mutation({
...
});

client code:
import { ConvexHttpClient } from "convex/browser";
import { api } from "./convex/_generated/api.js";

// HTTP client
const httpClient = new ConvexHttpClient('https://something.convex.cloud');
httpClient.mutation(api.connectionStorage.set, {
...
});

The convex client is within a Node.js app, and there's an error while compiling it says "module not found" for:
import { api } from "./convex/_generated/api.js";

This makes sense to me because the client has no idea about the structure of Convex API, so how do I call that mutation in Convex?

raw wraithBOT
#

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

#

To call a Convex mutation from a Node.js client, you're on the right track, but there are a few adjustments needed. The error you're encountering is because the client doesn't have access to the generated API file. Here's how you can resolve this:

  1. First, ensure you're running npx convex dev in your Convex project directory. This command generates the necessary API files Convex JavaScript Clients (https://docs.convex.dev/client/javascript).

  2. If you're using CommonJS (require() syntax) in your Node.js app, you'll need to configure Convex to generate a CommonJS-compatible API file. Create or edit a convex.json file in your project root with the following content:

    {
      "generateCommonJSApi": true
    }
    

    This will generate an api_cjs.cjs file that you can import in CommonJS JavaScript with CommonJS (https://docs.convex.dev/client/javascript/node#javascript-with-commonjs-require-syntax).

  3. If your Node.js client is in a separate project or doesn't have access to the generated API file, you can use the anyApi object instead. Here's how you can modify your client code:

    const { ConvexHttpClient } = require("convex/browser");
    const { anyApi } = require("convex/server");
    
    const httpClient = new ConvexHttpClient('<https://something.convex.cloud'>);
    httpClient.mutation(anyApi.connectionStorage.set, {
      // ... your mutation arguments
    });
    

    This approach allows you to call Convex functions without the generated API file Using Convex without generated api.js (https://docs.convex.dev/client/javascript#using-convex-without-generated-convex_generatedapijs).

  4. If you prefer to use ES modules (import/export syntax), make sure your package.json includes "type": "module", and then you can use the following import statement:

#
   import { ConvexHttpClient } from "convex/browser";
   import { anyApi } from "convex/server";

Node.js (https://docs.convex.dev/client/javascript/node)

Remember to replace '<https://something.convex.cloud'> with your actual Convex deployment URL.

By using these approaches, you should be able to call your Convex mutation from your Node.js client without needing direct access to the generated API file.

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