{ "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\n" } ], "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": "\n

As well as fetching API data<\/a>, you can also use Astro to generate your own API endpoints.<\/p>\n\n

This is an example of an endpoint that I recently created as part of a demo application:<\/p>\n\n

\/\/ trains.json.ts\n\nimport data from \"@\/data.json\";\nimport type { APIRoute } from \"astro\";\nimport type { Train } from \"@\/types\";\n\nexport const get: APIRoute = () => {\n  return {\n    body: JSON.stringify(data.trains as Train[]),\n  };\n};\n<\/code><\/pre>\n\n

The train data is imported from a JSON file, and Astro's APIRoute<\/code> is responsible for setting the appropriate response headers.<\/p>\n\n

For server-side rendered applications, you can also have endpoints for post<\/code>, del<\/code> and all<\/code>, though for this example, I only needed to support GET requests.<\/p>\n\n

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.<\/p>\n\n

And, as the example application that consumes the data is also written with Astro, having them both using the same solution has advantages too.<\/p>\n\n ", "format": "full_html", "processed": "\n

As well as fetching API data<\/a>, you can also use Astro to generate your own API endpoints.<\/p>\n\n

This is an example of an endpoint that I recently created as part of a demo application:<\/p>\n\n

\/\/ trains.json.ts\n\nimport data from \"@\/data.json\";\nimport type { APIRoute } from \"astro\";\nimport type { Train } from \"@\/types\";\n\nexport const get: APIRoute = () => {\n  return {\n    body: JSON.stringify(data.trains as Train[]),\n  };\n};\n<\/code><\/pre>\n\n

The train data is imported from a JSON file, and Astro's APIRoute<\/code> is responsible for setting the appropriate response headers.<\/p>\n\n

For server-side rendered applications, you can also have endpoints for post<\/code>, del<\/code> and all<\/code>, though for this example, I only needed to support GET requests.<\/p>\n\n

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.<\/p>\n\n

And, as the example application that consumes the data is also written with Astro, having them both using the same solution has advantages too.<\/p>\n\n ", "summary": null } ], "feeds_item": [ { "imported": "1970-01-01T00:33:45+00:00", "guid": null, "hash": "c6a3315ecb8173fc096250981c691fbe", "target_type": "feeds_feed", "target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76" } ] }