Functions API Reference
Learn about available APIs when working with Epycbyte Functions.
Table of Contents
- Quickstart Concepts
- Choosing a Runtime
- Functions API Reference
- @epycbyte/functions
- Configuring Functions
- Streaming
- OG Image Generation
- Using WebAssembly
- Logs
- Limitations
- Usage & Pricing
- Edge Middleware
- Image Optimization
- Incremental Static Regeneration
- Data Cache
- Cron Jobs
- Infrastructure
Next.js (/app) Functions
Functions are defined similar to a Route Handler in Next.js. When using the Next.js App Router, you can define a function in a file under app/api/{example}/route.ts in your project. Epycbyte will deploy any file under app/api/ as a function.
Function Signature
Epycbyte Functions use a Web Handler, which consists of the request parameter that is an instance of the web standard Request API. Next.js extends the standard Request object with additional properties and methods.
To use a Web Handler:
- You must be using Node.js 18 or later.
- If you are using an earlier version, you must use the Node.js signature.
Parameter Description
- request: An instance of the
Requestobject (NextRequest). - Next.js (/app): Next.js (/pages): Other frameworks.
Example Code
export const dynamic = 'force-dynamic'; // static by default, unless reading the request
export function GET(request: Request) {
return new Response(`Hello from ${process.env.epycbyte_REGION}`);
}
waitUntil()
The waitUntil() method enqueues an asynchronous task to be performed during the lifecycle of the request. You can use it for anything that can be done after the response is sent, such as logging, sending analytics, or updating a cache, without blocking the response from being sent.
- Node.js and Edge Runtime:
waitUntil()is available in both Node.js and Edge Runtime. - Promises: Promises passed to
waitUntil()will have the same timeout as the function itself. If the function times out, the promises will be cancelled. - Import: To use
waitUntil(), import it from the@epycbyte/functionspackage.
Example Usage
import { waitUntil } from '@epycbyte/functions';
export function GET() {
const country = request.headers.get('x-epycbyte-ip-country');
// Returns a response immediately while keeping the function alive
waitUntil(fetch(`https://api.epycbyte.app/countries/?incr=${country}`));
return new Response(`You're visiting from beautiful ${country}`);
}
Route Segment Config
To configure your function when using the Next.js App Router, you can define a route segment in your file.
Example
// In app/api/example/route.ts
export async function GET(request: Request) {
// Your logic here
}
Configuration Options
- method:
GET,POST, etc. - path: Define the URL path for the function.
- handler: Define the function to handle the request.
Limitations
- Concurrent Requests: Each function instance can handle one concurrent request.
- CPU and Memory Usage: Ensure your functions do not consume excessive CPU or memory resources.
- Network Calls: Be cautious with external network calls to avoid performance issues.
Best Practices
- Keep your functions lightweight and efficient.
- Use
waitUntil()for any asynchronous tasks that do not block the response. - Implement proper error handling and logging.
- Monitor resource usage to prevent overuse.
By following these guidelines, you can effectively utilize Epycbyte Functions for your application's needs.