Skip to main content

Introduction

Velaria uses Astro API routes to handle server-side functionality. These endpoints are built using Astro’s file-based routing system and provide RESTful interfaces for client-server communication.
All API routes are server-side rendered (SSR) and use export const prerender = false to ensure dynamic handling of requests.

Available endpoints

The Velaria API currently provides the following endpoints:
EndpointMethodDescription
/api/contactPOSTSubmit contact form data and send email notifications

How Astro API routes work

Astro API routes are defined in the src/pages/api/ directory. Each file exports HTTP method handlers (GET, POST, PUT, DELETE, etc.) that process requests and return responses.

Basic structure

import type { APIRoute } from "astro";

export const POST: APIRoute = async ({ request }) => {
  const body = await request.json();
  
  // Process the request
  
  return new Response(
    JSON.stringify({ success: true }),
    { status: 200 }
  );
};

Response format

All API endpoints return JSON responses with a consistent structure:
success
boolean
required
Indicates whether the request was processed successfully
msg
string
required
A message describing the result or error

Success response

{
  "success": true,
  "msg": "Mensaje recibido"
}

Error response

{
  "success": false,
  "msg": "Por favor, llena el formulario"
}

Status codes

The API uses standard HTTP status codes:
  • 200: Success - Request processed successfully
  • 500: Server Error - Validation failed or processing error occurred
Currently, validation errors return a 500 status code. In a production environment, consider using 400 (Bad Request) for client-side validation errors.

Authentication and security

The current API endpoints do not require authentication. All endpoints are publicly accessible.

Security considerations

  • CORS: Endpoints are accessible from the same origin by default
  • Rate limiting: Not currently implemented
  • Input validation: Server-side validation is performed on all inputs
  • Email validation: Uses regex pattern /^[^\s@]+@[^\s@]+\.[^\s@]+$/
For production deployments, consider implementing rate limiting to prevent abuse and adding authentication for sensitive endpoints.

Error handling

All endpoints use try-catch blocks to handle errors gracefully. When an error occurs:
  1. The error is logged to the console
  2. A JSON response is returned with success: false
  3. An appropriate status code is set (typically 500)
  4. A user-friendly error message is provided in the msg field

External integrations

The Velaria API integrates with external services:
  • Email API: https://email-api-bj45.vercel.app/api/mail/contact - Used to send contact form submissions via email
Ensure the external email API is available and properly configured. If the email service is down, contact form submissions will fail.

Making requests

To interact with the API, make HTTP requests from your client-side code:
const response = await fetch('/api/contact', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'John',
    lastName: 'Doe',
    email: '[email protected]',
    phone: '1234567890',
    message: 'I would like to order custom candles'
  })
});

const data = await response.json();
console.log(data);

Next steps

Contact endpoint

Learn how to submit contact form data

Build docs developers (and LLMs) love