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

Endpoints

Endpoints allow you to create custom API routes that execute your JavaScript code inside a secure sandbox environment.

Each endpoint represents an HTTP API that can receive requests, process data, interact with external services, use the built-in JSON Database, and return a response.

Creating an Endpoint

To create a new endpoint, configure the following options:

Endpoint Name

Each endpoint has a Name field.

The name is a friendly identifier used to recognize and manage your endpoint in the dashboard.

Endpoint HTTP Method

Each endpoint has an HTTP Method option.

The selected method determines which type of HTTP request can trigger the endpoint, such as GET, POST, PUT, PATCH, or DELETE.

Endpoint Pathname

All endpoint URLs are automatically converted to the following format:

http://127.0.0.1:3000/api/v1{your-endpoint-pathname}

The {your-endpoint-pathname} part is replaced with the path of your created endpoint.

If your endpoint pathname is:

/users

The generated URL will be:

http://127.0.0.1:3000/api/v1/users

If your endpoint pathname is:

/products/:id

The generated URL will be:

http://127.0.0.1:3000/api/v1/products/:id

If your endpoint pathname is:

/auth/login

The generated URL will be:

http://127.0.0.1:3000/api/v1/auth/login

Use the generated URL to send requests from your application or API client.

Endpoint Active Status

Each endpoint has an Active checkbox.

The endpoint will only process requests when it is active.

  • Active: The endpoint is enabled and can receive requests.
  • Inactive: The endpoint is disabled and requests will not be executed.

If an endpoint is not active, EndpointX will not run your code and the API request will fail.

Make sure to enable the Active option after creating or updating an endpoint.

Endpoint Code

Each endpoint contains a JavaScript function that handles incoming requests.

The function name must match the configured HTTP method and must always be asynchronous.

Example:

export async function GET() {
  return {
    status: 200,
    body: {
      message: 'Hello from EndpointX',
    },
  };
}

The returned object must always include:

  • status: The HTTP status code returned to the client.

Endpoint code must export exactly one asynchronous function that matches the selected HTTP Method. If the exported function name does not exactly match the method assigned to the endpoint, EndpointX will not execute it.

// correct function for POST request ✅
export async function POST() {
  // write your code here
}
// incorrect functions for POST request ❌
export async function handler() {}
async function POST() {} // the function is not exported.

Response data

The response object supports the following properties:

  • status - Required. The HTTP status code returned to the client.
  • db - Optional. Include this property when you modify the database so EndpointX can detect and persist the changes.
  • headers - Optional. Include this property when you modify the response headers so EndpointX can detect and send them to the client.
  • body - Optional. The response payload returned to the client. Must be an object or an array.

Request Data

Endpoints can access incoming request data:

  • headers - Request headers
  • params - Dynamic route parameters
  • searchParams - URL query parameters
  • body - JSON request body

Example:

export async function POST() {
  const name = body.name;
 
  return {
    status: 200,
    body: {
      message: `Hello ${name}`,
    },
  };
}

External API Requests

You can call external APIs directly from your endpoint using the built-in axios client.

Example:

export async function GET() {
  const response = await axios({
    method: 'GET',
    url: 'https://jsonplaceholder.typicode.com/users',
  });
 
  return {
    status: 200,
    body: response.data,
  };
}

Database Access

EndpointX provides a built-in JSON Database through the global db object.

You can read and modify data directly:

export async function POST() {
  db.users.push({
    name: 'John',
  });
 
  return {
    status: 201,
    db,
    body: {
      message: 'User created',
    },
  };
}

If you modify the database, you must include db in your response so EndpointX can detect and persist the changes.

Testing an Endpoint

After creating an endpoint:

  1. Copy the generated endpoint URL.
  2. Send a request using your preferred HTTP client.
  3. Check the response and execution logs in the dashboard.

Live Logs

All endpoint execution logs are available in Live Logs.

You can use:

console.log('Debug message');

to track execution flow, inspect variables, and debug your endpoint behavior.

Live Logs are available from the dashboard and update while your endpoint is running.

Security

Endpoints run inside an isolated sandbox environment.

You can use environment variables, request headers, and the database as needed. However, never use or sensitive information.

Practical Examples

Below are common examples of how to build CRUD (Create, Read, Update, Delete) operations using the EndpointX JSON Database.

export async function GET() {
  // Initialize the collection if it doesn't exist
  if (!db.customers) db.customers = [];
 
  const categoryFilter = searchParams.category;
  let results = db.customers;
 
  if (categoryFilter) {
    results = results.filter((c) => c.category === categoryFilter);
  }
 
  return {
    status: 200,
    body: {
      count: results.length,
      data: results,
    },
  };
}
export async function POST() {
  const payload = body;
 
  if (!payload || !payload.email) {
    return {
      status: 400,
      body: { error: 'Email address is required.' },
    };
  }
 
  // Initialize the collection if it doesn't exist
  if (!db.customers) db.customers = [];
 
  const newCustomer = {
    id: UUID(),
    email: payload.email,
    profile: {
      firstName: payload.firstName || 'Unknown',
      lastName: payload.lastName || 'Unknown',
    },
    createdAt: new Date().toISOString(),
  };
 
  db.customers.push(newCustomer);
 
  return {
    status: 201,
    body: newCustomer,
    db,
  };
}
export async function PUT() {
  const targetId = params.id;
  const payload = body;
 
  if (!db.customers) db.customers = [];
 
  const customerIndex = db.customers.findIndex((c) => c.id === targetId);
 
  if (customerIndex === -1) {
    return {
      status: 404,
      body: { error: `Customer ${targetId} not found.` },
    };
  }
 
  // Update the record
  db.customers[customerIndex] = {
    ...db.customers[customerIndex],
    ...payload,
    updatedAt: new Date().toISOString(),
  };
 
  return {
    status: 200,
    body: db.customers[customerIndex],
    db,
  };
}
export async function DELETE() {
  const targetId = params.id;
 
  if (!db.customers) db.customers = [];
 
  const initialCount = db.customers.length;
  db.customers = db.customers.filter((c) => c.id !== targetId);
 
  if (db.customers.length === initialCount) {
    return {
      status: 404,
      body: { error: 'Customers not found.' },
    };
  }
 
  return {
    status: 200,
    body: { message: 'Customers deleted successfully.' },
    db,
  };
}