Getting the file’s metadata

To get the details of a file you can use the Get File API endpoint.

You can also use this endpoint to retrieve a new temporary pre-signed URL to download a file if the previous pre-signed URL has already expired.

import { Client } from '@botpress/client'

const client = new Client({
  token: process.env.BOTPRESS_PAT,
  botId: process.env.BOTPRESS_BOT_ID,
})

const file = await client.getFile('YOUR_FILE_ID')

Listing existing files of a bot

To list all the files of a bot you can use the List Files API endpoint.

const res = await client.listFiles()
const files = res.data.files

Filtering by tags

If you need to filter files by tags, you can just pass the tags parameter which should be an object with key-value pairs of tags a file must have in order to be returned. Tag filtering works in an “AND” fashion, so only the files that have all the specified tags will be returned.

const { files } = await client.listFiles({
  tags: {
    category: 'Sales',
  },
})

Pagination

The List Files API endpoint will return by default the 20 most recent files your bot has. If you need to list older files you can use the nextToken property returned in the API response to retrieve the next page (if any) of files. The nextToken will be included for each page if there are files remaining to be listed.

For example:

let res = await client.listFiles()
const files = res.data.files

// You can put this in a loop to retrieve all the files if needed.
if (res.data.nextToken) {
  res = await client.listFiles({ nextToken: res.data.nextToken })
  files.push(...res.data.files)
}

Updating the file metadata

Only the tags and access policies of a file can be updated.

Here’s an example of how to update the access policies and tags of a file using the Botpress Client:

await client.updateFile({
  id: 'YOUR_FILE_ID',
  accessPolicies: ['integrations'], // This value will replace the existing access policies of the file.
  tags: {
    // This acts as a "patch" or partial update, so only the tags specified here will be updated, the rest will remain unchanged. If you need to delete an existing tag, you can set it to a `null` value.
    category: 'Support', // This tag will be updated.
    knowledgeBaseName: null, // This tag will be deleted.
    subcategory: 'Technical', // This tag will be added.
    // Any other tags not specified here will remain unchanged.
  },
})

Updating file content

If you need to update the content of a file, you can create a new file with the updated content and then delete the old file. The file ID will change in this case.

Deleting a file

To delete a file you can use the “Delete File” API endpoint.

await client.deleteFile('YOUR_FILE_ID')