#How to determine if i'm running locally or in production in an API route?
3 messages · Page 1 of 1 (latest)
Hi @slender bay. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can determine if your site is running locally or in production by using the built-in environment variables import.meta.env.PROD and import.meta.env.DEV.
import.meta.env.PROD is true if your site is running in production and false otherwise. On the other hand, import.meta.env.DEV is true if your site is running in development and false otherwise. These variables are always the opposite of each other.
Here's how you can use them in your API route:
import type { APIRoute } from 'astro';
export const get: APIRoute = async function get() {
const isProd = import.meta.env.PROD;
const isDev = import.meta.env.DEV;
// Your code here...
};
In this code, isProd will be true if the site is running in production and isDev will be true if the site is running in development.
Please note that these environment variables are provided by Astro out-of-the-box and you can use them like any other environment variables.