oliverdavies.uk/content/node.4de8ccc0-035a-46b9-870d-6df54238eb65.yml

95 lines
3.9 KiB
YAML

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: |
<p>As well as <a href="/daily/2023/02/08/fetching-api-data-with-astro">fetching API data</a>, you can also use Astro to generate your own API endpoints.</p>
<p>This is an example of an endpoint that I recently created as part of a demo application:</p>
<pre><code class="javascript">// trains.json.ts
import data from "@/data.json";
import type { APIRoute } from "astro";
import type { Train } from "@/types";
export const get: APIRoute = () =&gt; {
return {
body: JSON.stringify(data.trains as Train[]),
};
};
</code></pre>
<p>The train data is imported from a JSON file, and Astro's <code>APIRoute</code> is responsible for setting the appropriate response headers.</p>
<p>For server-side rendered applications, you can also have endpoints for <code>post</code>, <code>del</code> and <code>all</code>, though for this example, I only needed to support GET requests.</p>
<p>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>
<p>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>
format: full_html
processed: |
<p>As well as <a href="http://default/daily/2023/02/08/fetching-api-data-with-astro">fetching API data</a>, you can also use Astro to generate your own API endpoints.</p>
<p>This is an example of an endpoint that I recently created as part of a demo application:</p>
<pre><code class="javascript">// trains.json.ts
import data from "@/data.json";
import type { APIRoute } from "astro";
import type { Train } from "@/types";
export const get: APIRoute = () =&gt; {
return {
body: JSON.stringify(data.trains as Train[]),
};
};
</code></pre>
<p>The train data is imported from a JSON file, and Astro's <code>APIRoute</code> is responsible for setting the appropriate response headers.</p>
<p>For server-side rendered applications, you can also have endpoints for <code>post</code>, <code>del</code> and <code>all</code>, though for this example, I only needed to support GET requests.</p>
<p>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>
<p>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>
summary: null
field_daily_email_cta: { }