Runtime APIs
EndpointX executes your JavaScript code inside a secure, sandboxed environment. To allow your code to interact with the incoming HTTP request and perform useful operations, the runtime injects a specific set of global objects and helper functions.
This page documents all available APIs within the EndpointX runtime.
Request Context Objects
When an endpoint is triggered, EndpointX parses the incoming HTTP request and exposes its details through several global objects. These objects are available synchronously in your exported function.
Platform Objects
These objects allow your endpoint to interact with EndpointX platform features.
db
The global db object provides direct, synchronous access to the EndpointX JSON Database. You can read, mutate, and delete properties on this object just like a standard JavaScript dictionary.
Database changes are not persisted automatically. If you modify the db object during execution, you must include db in the returned response object so the system can detect and persist the changes.
Example:
export async function POST() {
if (!db.users) db.users = [];
db.users.push({
name: 'John',
});
return {
status: 200,
db,
body: {
message: 'User added',
},
};
}See the related docs for detailed usage and examples.
params
The params object contains dynamic path parameters from the endpoint URL.
export async function GET() {
const userId = params.id;
return {
status: 200,
body: {
userId,
},
};
}searchParams
The searchParams object contains URL query parameters.
export async function GET() {
const role = searchParams.role;
const limit = searchParams.limit;
return {
status: 200,
body: {
role,
limit,
},
};
}body
The body object contains the request payload sent by the client.
It is commonly used with methods like POST, PUT, DELETE , and PATCH.
export async function POST() {
const name = body.name;
const email = body.email;
return {
status: 201,
body: {
message: 'User created',
user: {
name,
email,
},
},
};
}Make sure your client sends valid JSON when using the body object.
process.env
The process.env object provides access to your custom Environment Variables. This is the recommended way to store API keys and configuration flags.
See: the related docs for configuration instructions.
Headers
EndpointX provides access to request headers inside your endpoint code.
Custom Headers
Custom headers must start with the x-app- prefix.
Example request header:
x-app-user-id: 12345You can access custom headers from the headers object:
export async function GET() {
const userId = headers['x-app-user-id'];
return {
status: 200,
body: {
userId,
},
};
}Modifying Response Headers
You can add custom response headers by returning them in the response object.
export async function GET() {
return {
status: 200,
headers: {
'x-app-version': '1.0.0',
},
body: {
message: 'Header added successfully',
},
};
}When you add or modify response headers, you must include them in the returned response object so EndpointX can detect and apply the changes.
Default Headers
EndpointX automatically provides the following request headers:
user-agentaccept-languagereferer
You can read these headers directly from the headers object.
Helper Functions
Because the sandbox restricts access to standard Node.js modules, EndpointX provides built-in utilities for common backend tasks.
axios(config)
Makes outbound HTTP requests to external APIs using Axios.
Axios is available globally inside your endpoint sandbox, so you can use it directly without installing any packages.
It supports standard Axios configuration options such as:
url- The target API URLmethod- HTTP method (GET,POST,PUT,PATCH,DELETE, etc.)headers- Custom request headersparams- URL query parametersdata- Request body payload
Example:
export async function GET() {
try {
const response = await axios({
method: 'GET',
url: 'https://jsonplaceholder.typicode.com/users/1',
});
return {
status: 200,
body: response.data,
};
} catch (error) {
console.log('Request failed:', error.message);
return {
status: 500,
body: {
error: 'Failed to fetch user data.',
},
};
}
}UUID()
Generates and returns a universally unique identifier (UUID v4) as a string. Ideal for assigning IDs to new database records.
export async function POST() {
const userId = UUID();
db.users.push({
id: userId,
name: 'John',
});
return {
status: 201,
db,
body: {
message: 'User created successfully',
id: userId,
},
};
}signJWT(payload, secret)
Asynchronously generates a JSON Web Token.
payload(Object): The data you want to embed in the token.secret(String): The cryptographic key used to sign the token.
export async function POST() {
const token = await signJWT(
{
userId: '123',
role: 'admin',
},
'my-secret-key',
);
return {
status: 200,
body: {
token,
},
};
}jwtVerify(token, secret)
Asynchronously verifies a JSON Web Token and returns the decoded payload. Throws an error if the token is invalid or expired.
token(String): The JWT string to verify.secret(String): The cryptographic key used to verify the token.
export async function GET() {
try {
const payload = await jwtVerify('your-jwt-token', 'my-secret-key');
return {
status: 200,
body: {
user: payload,
},
};
} catch (error) {
return {
status: 401,
body: {
error: 'Invalid or expired token.',
},
};
}
}console.log(...args)
Writes output to the EndpointX execution logs.
The logs are available in Live Logs from the dashboard, making it useful for debugging incoming parameters, inspecting data, or tracking endpoint execution flow.
Example:
export async function GET() {
console.log('Endpoint started');
const userId = '123';
console.log('User ID:', userId);
return {
status: 200,
body: {
message: 'Success',
},
};
}Response Formatting Rules
Your exported function must always return an object containing a valid status property.
The status property defines the HTTP status code that EndpointX sends back to the client. The response payload should be placed inside the body property.
If your code executes multiple logical branches, make sure every possible execution path returns a valid response object with a status.
If the runtime reaches the end of your function without returning a valid object or status, the endpoint will throw an error.