How to expose an API-backed “virtual” collection in Forest Admin Agent?

Hello Forest Admin team,

I’m migrating my setup to the new Agent/Datasource architecture and need to add a collection that doesn’t live in Mongo (or any DB), but is fetched dynamically from a third-party API. In my old Forest Express/Mongoose integration I could do:

import { collection } from 'forest-express-mongoose';

collection('myapidata', {
  // … fields, actions, segments …
});

router.get('/myapidata', async (req, res) => {
//code to retreive the api data
}

With the Agent, I tried to start with:

export default function configureMyApiDataCollection(agent: Agent): void {
  agent.customizeCollection('myapidata', collection => {
    // … 
  });
}

But at runtime I get errors indicating the Agent “doesn’t know” the myapidata collection, and TypeScript complains that customizeCollection only works for collections that were added via addDataSource.

What’s the recommended pattern to:

  1. Declare a “virtual” collection whose data comes entirely from my external API
  2. Expose it in the Forest Admin UI with full list/detail views (and optional Smart Actions)
  3. Support Create/Update/Delete overrides (so CRUD operations call my API) - I used to have smart actions to do this

I’ve looked at both the Replication and Translation strategies in the docs, but I’m unclear how to wire a pure-API datasource into createAgent().addDataSource(...) so that customizeCollection('myapidata', …) starts working.

Could you please share a minimal example of how to do so.

Thank you in advance for your guidance!

Node.js Agent ^1.59.2
@forestadmin/agent / @forestadmin/datasource-mongoose / TypeScript setup

Hello @Ilyas_li and welcome to the community !

You were indeed on the right track, to have a more fleshed out example on how to implement those, you can take a look at the github repository of our agent, in which we have created an example project making use of the Translation strategy.

Everything is defined in this single file. Then you can import the factory to add the datasource to your agent.

Hope this helps !

Best regards,

1 Like

thank you, it worked

1 Like