set up an API call to firestore database
# 🤝help
i
Hi, I want to write/read from a firestore database. I am using botpress studio/cloud (the web version). For now I tried to add a card that executes a js code which does the API call. However when I try to do something like : import admin from 'firebase-admin'; It doesn't work because I don't have the dependency. How can I add dependencies on the studio versio of botpress? is it even possible ?
d
i suggest you setup an api like nodejs for exzmple and you import whatever you want there, and then call this api from botpress
i
so I need a new server in this case ?
is it impossible to have a dependency in botpress?
a
Hi @important-dawn-82568, right now Botpress doesn't support importing custom dependencies. The best solution would be, as Lawani says, for you to: 1. Set up a separate Node.js server that can connect to firebase and run operations. 2. Expose an API endpoint on that server 3. In Botpress, make a GET or POST request to that server to get your info
i
Okey I see, thanks for the help
hey I'm sorry to be back on this, I tried to do the solution that you told me, here's the server code that I wrote:
Copy code
js
const express = require("express");
const admin = require("firebase-admin");
const serviceAccount = require("./credentials.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

const app = express();

app.post("/write-data", async (req, res) => {
  try {
    const db = admin.firestore();
    const collectionRef = db.collection("collection_name");
    const documentData = req.body;

    const result = await collectionRef.add(documentData);

    res.json({ success: true, documentId: result.id });
  } catch (error) {
    console.error("Error writing document:", error);
    res
      .status(500)
      .json({ success: false, error: "Failed to write document to Firestore" });
  }
});

const port = 3000; // Use the port provided by the environment or default to 3000

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});
and the code that I am trying to run from botpress (the api call to the node.js server):
Copy code
js
// Set the API endpoint URL
const apiUrl = 'http://localhost:3000/write-data'; // Replace with the actual URL of your server

// Prepare the request body
const requestBody = {
  name: "test-name",
  age: "test-age"
};

// Make the API request
fetch(apiUrl, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(requestBody)
})
  .then(response => {
    if (response.ok) {
      // Handle successful API response
      return response.json();
    } else {
      // Handle API error response
      throw new Error('API request failed');
    }
  })
  .then(data => {
    // Handle the API response data
    console.log('API response:', data);
    // Do further actions or send a message based on the response
  })
  .catch(error => {
    console.error('Error calling API:', error);
    // Handle the error
  });

return {};
I keep getting this error :
22:32:41errorError executing action "inline-ins-3185f9d2f8.js" in flow:Employeur-français:node:Tell_info [ReferenceError, require is not defined]
and I don't get why... there is no import in the code that I run in botpress
a
I can think of a coupe things: 1. Are you hosting your server anywhere? I don't think BP can connect to your localhost; you'd need to expose the endpoint publically. 2. Have you tried making the API call in an app like Postman to make sure that it works?
78 Views