Expose external_id in embed.js

I am needing to load posts on an external site where I don’t know the post_id but I DO know the external_id that I configured when I created the topic.

Can someone please expose the external_id variable in the embed.js logic or teach me what I’m missing?

Example support needed:

window.DiscourseEmbed = {
discourseUrl: ‘https://forums.blah.org/’,
external_id: ‘2023-4’
};

I’m assuming you created the topics via the API and set the external_id parameter.

I’m not sure if this helps, but Discourse has a t/external_id/:external_id route. It looks like it only returns JSON data:

So you can get the topic’s JSON at (for example )

/t/external_id/2023-4.json

That would return the content and id of the Discourse topic. I don’t think this solves your problem though.

Testing this now, JSON data is returned, but Discourse performs an automatic redirect to the actual Discourse topic URL when you make the request. For example a GET request to http://localhost:4200/t/external_id/1.json redirects me to http://localhost:4200/t/testing-the-external-id-param/206.json

I’m looking to have something in the formatted form of embedded posts. Discourse does a great job with that layout already and I don’t want to reinvent the wheel.

By not allowing to search by external_id, I would have to store the forum topic id in my db and search that way. Seems redundant when we already have a unique variable stored in external_id when I created the topic from the API.

1 Like

I think I understand now. You are wanting to use the script that’s shown here: https://meta.discourse.org/t/embed-discourse-comments-on-another-website-via-javascript/31963#alternate-configuration-linking-to-existing-topics-6. But instead of using the topicId parameter you want to use the external_id parameter.

Possibly this could be added as a feature.

It might also be possible to make a request on the client to (for example)
http://localhost:4200/t/external_id/1.json
Then extract the topic’s actual URL from the response and either parse that URL to extract the topic’s id, or make a second request to the topic’s URL to get the topic id.
This seems likely to be error prone.

If you’re adding the embed script on the server instead of the client, you could just make a GET request to http://localhost:4200/t/external_id/1.json, perform any actions you need to get the Discourse topic’s id, then insert the script onto your web page using that id.

This is good feedback and gives me some things to mull over. The feature request is most straightforward but I like some of your ideas that I hadn’t thought of!

1 Like