About EndpointX

EndpointX is an open-source project available on GitHub.

You can view the source code, report issues, suggest ideas,
or contribute to the project

https://github.com/robnsiov/endpointx

If you find a bug, have an improvement idea, want to help
improve the documentation, or have any feedback, feel free to contact me.

Email: robnsiov@gmail.com

Thank you for supporting open-source development ❤️ .

Ctrl+K

Environment Variables

Environment variables allow you to store sensitive information, configuration settings, and API keys outside of your endpoint code. In EndpointX, custom environment variables are injected directly into your secure sandbox runtime and are accessible during code execution.

This prevents hardcoding secrets directly into your JavaScript logic and makes it easy to switch configurations without modifying your endpoint code.

Limits

You can create up to 30 custom environment variables per project.

Use environment variables only for required configuration values, API keys, and sensitive data. Avoid storing large amounts of data or using environment variables as a database.

Accessing Variables

You can access your custom environment variables globally through the process.env object.

Real-World Examples

Below are practical implementations demonstrating how to utilize environment variables in your endpoints alongside other sandbox features.

export async function GET() {
  // IMPORTANT:
  // You must sign up at https://www.xweather.com
  // and create API credentials to use this service.
  // Add your client ID and client secret as environment variables.
 
  const clientId = process.env.XWEATHER_CLIENT_ID;
  const clientSecret = process.env.XWEATHER_CLIENT_SECRET;
 
  if (!clientId || !clientSecret) {
    console.log('Missing XWeather API credentials');
 
    return {
      status: 500,
      body: {
        error: 'Server configuration error.',
      },
    };
  }
 
  const city = searchParams.city || 'New York, NY';
 
  try {
    const response = await axios({
      method: 'GET',
      url: `https://data.api.xweather.com/conditions/${encodeURIComponent(city)}`,
      params: {
        client_id: clientId,
        client_secret: clientSecret,
      },
    });
 
    return {
      status: 200,
      body: response.data,
    };
  } catch (error) {
    console.log('Weather API request failed:', error.message);
 
    return {
      status: 502,
      body: {
        error: 'Failed to fetch weather data from upstream service.',
      },
    };
  }
}
export async function POST() {
  const jwtSecret = process.env.JWT_SECRET;
  const payload = body;
 
  if (!jwtSecret) {
    return {
      status: 500,
      body: { error: 'Server configuration error: Missing JWT secret.' },
    };
  }
 
  if (!payload || !payload.username) {
    return {
      status: 400,
      body: { error: 'username is required in request body.' },
    };
  }
 
  const sessionId = UUID();
  const token = await signJWT(
    {
      user: payload.username,
      session: sessionId,
      role: 'standard',
    },
    jwtSecret,
  );
 
  return {
    status: 200,
    body: {
      accessToken: token,
      sessionId: sessionId,
    },
  };
}
export async function POST() {
  const isDebug = process.env.DEBUG_MODE === 'true';
  const payload = body;
 
  if (!payload || !payload.eventName) {
    return {
      status: 400,
      body: { error: 'eventName is required.' },
    };
  }
 
  // Store in JSON Database
  if (!db.analytics) db.analytics = [];
 
  const newEvent = {
    id: UUID(),
    event: payload.eventName,
    pathId: params.id || null,
    createdAt: new Date().toISOString(),
  };
 
  db.analytics.push(newEvent);
 
  if (isDebug) {
    console.log('Debug: New analytics event created with ID', newEvent.id);
    console.log('Debug: Incoming headers:', headers);
  }
 
  return {
    status: 201,
    body: newEvent,
    db,
  };
}

Best Practices

Failing Gracefully Always verify that your required environment variables exist before executing critical logic. If an environment variable is missing (e.g., it was not configured or misspelled), process.env.KEY_NAME will evaluate to undefined.

Database Seeding You can use environment variables to hold default administrative keys or configuration flags that determine how the db object should be initialized upon the first execution.