1

Khan Academy's API Explorer has an exercises section that mentions filtering by tags, but the url with math tag applied returns nothing.

The generic exercise objects don't contain the topic they're in. My guess is that there's an id to join on somewhere in the topictree/exercises json objects, but I don't know an efficient way to find it.

Here are the raw exercises json and raw topictree json (note, the second one is huge, and contains many topics other than math).

Adam
  • 2,873
  • 3
  • 18
  • 17

2 Answers2

3

I don't think there is a nice way to return exercises from just a subtree of the topictree (e.g. just math). Tags are a different concept, and there isn't a tag common to everything in math. Probably your best bet is to load the full topictree with just Exercises (and Topics) and work from there:

http://www.khanacademy.org/api/v1/topictree?kind=Exercise

If you need to reference this structure repeatedly, it probably makes sense to download and filter it ahead of time, and maybe re-fetch it from time to time to account for changes to Khan Academy content. But it depends on your exact use case.

Generally, any content item can be referenced by content_id (sometimes just called id) or by slug, but unfortunately, the naming and usage aren't consistent everywhere.

Alan Pierce
  • 4,083
  • 2
  • 22
  • 21
  • Thanks Alan. While Jannie's answer was correct and included yours, I accepted yours because it got me exactly what I needed without any parts that didn't work. – Adam Oct 06 '15 at 21:26
3

You can use the following to get all the exercises:

http://www.khanacademy.org/api/v1/exercises
http://www.khanacademy.org/api/v1/topictree?kind=Exercise

I'm not sure what's the difference between these two - I don't use them. I prefer to fetch the data for the individual topic nodes as follows:

http://www.khanacademy.org/api/v1/topic/%s
http://www.khanacademy.org/api/v1/topic/%s/exercises
http://www.khanacademy.org/api/v1/topic/%s/videos

where %s is the "node_slug" property for each topic. The root of the tree is just "root". The first one will give you the topic details and a list of sub-items in the "child_data" array. Use the "id" properties of each sub-topic in this array to look up its details in the "children" array having "internal_id" equal to "id". There you get the "node_slug" to for the next API call for that sub-topic. The "child_data" array has all the sub-items in the order that they appear on the website when you're working with the missions.

I cache these responses so that I don't have to download everything every time.

Jannie Gerber
  • 659
  • 1
  • 7
  • 15
  • 1
    Thanks Jannie. I'm unsure how [topic/algebra/exercises](http://www.khanacademy.org/api/v1/topic/algebra/exercises) works for you. It returns an empty array for me. [topictree?kind=Exercise](http://www.khanacademy.org/api/v1/topictree?kind=Exercise) did return data to join by, but lacked exercise data like seconds_per_fast_problem. [exercises](http://www.khanacademy.org/api/v1/topic/algebra/exercises) returned an array joinable on IDs, urls, node_slug, and other properties. I ultimately matched on relative_url as a tradeoff between brevity and ease for me to grok while working with the data. – Adam Oct 06 '15 at 21:18
  • 1
    @Adam. You have to traverse the tree as I explained in my answer. Just use api/v1/topic/algebra to get the sub-topics under Algebra. They're in the "child_data" and "children" entries. Now do the same for each of these. Continue like that until you get the sub-topics that actually contain exercises (and videos). Then use the api/v1/topic/%s/exercises format for each of these to get the exercises. For the videos use api/v1/topic/%s/videos. – Jannie Gerber Oct 07 '15 at 12:49
  • 1
    How do you actually use these exercise entries? The questions from the exercise aren't listed – TheEnvironmentalist Oct 01 '17 at 02:28
  • I don't use the exercises via my app - I just wanted to explore the API and currently it only uses the API calls that don't require authorization. My app is not working properly at the moment - it doesn't handle some newer topics correctly - and I haven't really used it for a long time. – Jannie Gerber Oct 02 '17 at 09:33