Template for creating and testing ImageKit URL Endpoint Functions with unit tests and GitHub Actions CI.
- Node.js 14.x - Required for compatibility
npm install
npm testfunction handler(url, urlPrefix, context) {
// Your logic here
return { url: modifiedUrl }
}
// Important: Always export using this format
module.exports.handler = handler;Note: Always include module.exports.handler = handler; at the end of your handler file.
url- Full request URL including protocol, hostname, path, and query stringurlPrefix- Pattern identifier from client configurationcontext- Request context object:host- Request hostnameclientNumber- Client identifierisDebug- Debug mode flaglogger- Request logger (.info(),.warn(),.error(),.debug())
Normal Result (continue processing):
{
url: string, // Required: Full URL with protocol + hostname
signURL: boolean // Optional: Should server sign this URL? (default: false)
}Early Response (stop processing):
{
status: number, // Required: HTTP status code
body: string | object, // Required: Response body
headers: Record<string, string> // Optional: Response headers
}See the examples/ folder for ready-to-use handler implementations:
- Simple URL Rewrite - Change version in path (e.g., /v1/ to /v2/)
- Path Parameters - Extract path parameters and convert to query string
- Hostname Change - Change domain/hostname
- Keyword Path Rewriting - Rewrite paths based on keyword mapping
- Query Parameter Transformation - Transform custom query params
- Access Control - Block access to private paths and sensitive files
- Error Handling - Handle errors gracefully with fallback
- Video Thumbnail - Generate video thumbnails with image extensions
Each example is a complete, working handler file that you can copy directly to handler.js. See examples/README.md for details.
function handler(url, urlPrefix, context) {
return {
url: url.replace('/v1/', '/v2/')
};
}
module.exports.handler = handler;npm test # Run tests
npm run test:watch # Watch mode
npm run test:coverage # With coverageGitHub Actions automatically runs tests on push/PR to main or develop branches.