https://discord.gg/botpress logo
API to knowledge Bases
# 🤝help
h
I have a successful API connection, but I want to store the data in a Knowledge Base. I'm trying to do that using the Code module, but I can't find an example of the code or some related code in the documentation. How can I store the data in a Knowledge Base?
p
To add files to the knowledge base you should use the files API. Here's a code snippet I've successfully used to achieve a kb upload within a bot:
Copy code
js
const fileUrl = workflow.url[0] // Get the cleaned file URL
const knowledgeBaseId1 = '<your-kb-id>' // Assuming the KB1 knowledge base ID
const name = workflow.companyName

//Add the file to the knowledge base
const file = await client.uploadFile({
  key: `${knowledgeBaseId1}/${name}.pdf`, // Each file needs a unique key under your bot
  url: fileUrl, // This is the external URL where the file is currently located
  contentType: 'application/pdf',
  index: true,
  tags: {
      // Tags are optional but are useful for filtering files by a custom criteria you choose when using the "List Files" or "Search Files" API endpoints. You can change or remove the tags below based on your needs.
    knowledgeBaseName: 'brand-pdfs',
    source: 'knowledge-base',
    kbId: knowledgeBaseId1,
    title: workflow.companyName
  },
})
Here are the docs about the API: https://botpress.com/docs/api-reference/files-api/getting-started
5 Views