uuid: - value: 4de8ccc0-035a-46b9-870d-6df54238eb65 langcode: - value: en type: - target_id: daily_email target_type: node_type target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7 revision_timestamp: - value: '2025-05-11T09:00:46+00:00' revision_uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 revision_log: { } status: - value: true uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 title: - value: | Creating API endpoints with Astro created: - value: '2023-02-09T00:00:00+00:00' changed: - value: '2025-05-11T09:00:46+00:00' promote: - value: false sticky: - value: false default_langcode: - value: true revision_translation_affected: - value: true path: - alias: /daily/2023/02/09/creating-api-endpoints-with-astro langcode: en body: - value: |
As well as fetching API data, you can also use Astro to generate your own API endpoints.
This is an example of an endpoint that I recently created as part of a demo application:
// trains.json.ts
import data from "@/data.json";
import type { APIRoute } from "astro";
import type { Train } from "@/types";
export const get: APIRoute = () => {
return {
body: JSON.stringify(data.trains as Train[]),
};
};
The train data is imported from a JSON file, and Astro's APIRoute
is responsible for setting the appropriate response headers.
For server-side rendered applications, you can also have endpoints for post
, del
and all
, though for this example, I only needed to support GET requests.
This is something that I've used a db-json library for previously, but being able to do this in Astro seemed like a good fit as I can easily manage lists of stations as well as a single station from one JSON file but I can just take the static HTML that Astro generates and upload it to a static hosting solution which simplifies the hosting side of things a lot.
And, as the example application that consumes the data is also written with Astro, having them both using the same solution has advantages too.
format: full_html processed: |As well as fetching API data, you can also use Astro to generate your own API endpoints.
This is an example of an endpoint that I recently created as part of a demo application:
// trains.json.ts
import data from "@/data.json";
import type { APIRoute } from "astro";
import type { Train } from "@/types";
export const get: APIRoute = () => {
return {
body: JSON.stringify(data.trains as Train[]),
};
};
The train data is imported from a JSON file, and Astro's APIRoute
is responsible for setting the appropriate response headers.
For server-side rendered applications, you can also have endpoints for post
, del
and all
, though for this example, I only needed to support GET requests.
This is something that I've used a db-json library for previously, but being able to do this in Astro seemed like a good fit as I can easily manage lists of stations as well as a single station from one JSON file but I can just take the static HTML that Astro generates and upload it to a static hosting solution which simplifies the hosting side of things a lot.
And, as the example application that consumes the data is also written with Astro, having them both using the same solution has advantages too.
summary: null field_daily_email_cta: { }