I am developing a syntax checker and need to extract the body of a document, so I would like to retrieve the AST directly via typst engine.
I don’t think that’s possible within Typst. The closest thing within Typst is to prepend the document with #show: rest => [#metadata(yaml.encode(rest))<meta>] and execute typst eval --in main.typ 'query(<meta>).first().value'.
You can use external programs/libraries to get AST. Some of the projects listed in
Programming —
Compiling Typst documents | Best of Typst (TCDM) expose API for that.