Extract code blocks API
Ignore the chat text. Extract only the code snippets from an AI response.
| Endpoint | POST https://astraltext.com/api/v1/extract-code-blocks |
| Auth | Bearer token, free |
| Sends | input: text |
| Returns | result: text |
| Price | Free. 1,000 calls a day, 60 a minute. |
When to call it
When a model writes a file for you, the file is inside the answer and the answer is mostly explanation. Anything that takes model output and writes it to disk, runs it or diffs it needs the code alone, and it needs the same answer every time rather than whatever a regular expression happened to match.
For a one-off, the browser version needs no token and never sends your data anywhere: it runs on your own machine.
Example
curl
curl -X POST https://astraltext.com/api/v1/extract-code-blocks \
-H "Authorization: Bearer ast_YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"input": "Here is the fix:\n\n```ts\nexport const answer = 42;\n```\n\nAnd the test:\n\n```ts\nexpect(answer).toBe(42);\n```"}'response
{
"tool": "extract-code-blocks",
"result": "export const answer = 42;\n\n--------------------------------------------------\n\nexpect(answer).toBe(42);",
"chars": 103,
"ms": 1
}In your language
JavaScript
const res = await fetch("https://astraltext.com/api/v1/extract-code-blocks", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.ASTRAL_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({"input": "Here is the fix:\n\n```ts\nexport const answer = 42;\n```\n\nAnd the test:\n\n```ts\nexpect(answer).toBe(42);\n```"}),
});
if (!res.ok) throw new Error((await res.json()).detail);
const { result } = await res.json();Python
import os, requests
res = requests.post(
"https://astraltext.com/api/v1/extract-code-blocks",
headers={"Authorization": f"Bearer {os.environ['ASTRAL_TOKEN']}"},
json={"input": "Here is the fix:\n\n```ts\nexport const answer = 42;\n```\n\nAnd the test:\n\n```ts\nexpect(answer).toBe(42);\n```"},
timeout=30,
)
res.raise_for_status()
result = res.json()["result"]Parameters
| Field | Type | Notes |
|---|---|---|
| input | string | Required. Up to 200,000 characters per call. |
Errors
Errors are RFC 7807 problem documents with a stable type you can branch on.
| missing-token | 401 | No Authorization header. |
| bad-request | 400 | input missing or the wrong type. |
| tool-failed | 422 | The input could not be processed. detail says why. |
| quota-exceeded | 429 | Past 1,000 calls today. |
Questions
- What if there are several blocks?
- All of them come back, in order, separated by a blank line. Nothing is merged and nothing is chosen for you.
- Is the language tag kept?
- No. The fence and its language tag are markup, not code, so what comes back is what you would write to a file.
- What if the answer has no code at all?
- You get an empty result rather than an error. Check the chars field to decide whether there was anything to extract.
Other endpoints
- AI text cleaner API
- Remove em dashes API
- Remove invisible characters API
- Remove emoji API
- Remove line breaks API
- Strip Markdown API
A token takes thirty seconds
Free, no card, 1,000 calls a day, and the same token works on astraltext.com, astralpdf.com, astraljson.com and astralbatch.com.
Get a free token