Can I send an external URL to the Discourse API for it to return topics linking to that URL?

AI-generated summary

Users are discussing whether it’s possible to query the Discourse API with an external URL to return forum topics that link to it. Jonathan5 wants to display relevant forum topics on an external site. pfaffman suggests using the TopicLink table via the Data Explorer plugin or building a custom plugin, and references a GitHub integration that does something similar. Jonathan5 asks if Data Explorer queries with URL parameters can be run via the API, referencing a guide on running queries via API. merefield confirms it’s possible using Discourse’s built-in search API, e.g., search.json?q=URL, and recommends client-side JavaScript to parse results — though server-side caching is advised to avoid overloading Discourse. Andrew_Rowe confirms the search method works on his server and shares his experience using PHP and cURL to fetch results for AJAX calls, linking to his own topic. pfaffman reacts with realization that a simple search query already solves the problem — no complex plugin or query needed. In short, while no dedicated API endpoint exists for this, the search API (search.json) can effectively return topics containing a given URL, making it feasible to implement with some client- or server-side code.

Would it be possible, on an external non-Discourse website, to have a “Forum topics that discuss this page” section containing links to the relevant Discourse forum topics?

If the API can take a URL and return a list of topic IDs/URLs that would be perfect.

Do you know about Embed Discourse comments on another website via Javascript?

But if you think that multiple topics link to a page then you might be able to use the TopicLink table to do a search of topics that link that URL (probably with the Data Explorer plugin) and then you’d have the topic and post IDs so you could link to them.

I don’t think there’s an endpoint to get them. A plugin could make one available, I think. Also, Discourse GitHub doess something sort-of similar, so that might help.

Do you think I could create a query with a URL parameter and run it via the API using this? Run Data Explorer queries with the Discourse API.

Yes I think so, kinda.

Try this, e.g.:

https://meta.discourse.org/search.json?q=https%3A%2F%2Fnews.bbc.co.uk

Tidy results in JavaScript.

If anon can use search you perhaps don’t need an API key even (then you can put this client side)

However you might want to cache the results on the external server for best results and avoid poor Discourse get overwhelmed with search queries.

So this probably involves writing server side code on your external website for best outcome.

Wow, thanks Robert, this does work, I just tried on my own server.

This sounds really similar to something I was trying to do earlier. My other server uses PHP and I figured out how to use Curl commands in a PHP module to hit the API and return json results to an ajax call from the client. Here is that topic

Oh! Of course. A regular search will find the URL! :person_facepalming: