Get started with EdgeLock in just a few minutes.
First, you'll need an EdgeLock account and an API key.
Install the JavaScript/TypeScript SDK using your preferred package manager.
npm install @edgelock/client# oryarn add @edgelock/client# orbun add @edgelock/client
Set your API key as an environment variable for the SDK.
export EDGELOCK_KEY="your_api_key_here"
Alternatively, configure the client directly with the key (less recommended for production):
import { EdgeLock } from "@edgelock/client";// Configure the client onceEdgeLock.configure({apiKey: process.env.EDGELOCK_KEY || "your_api_key_here",});
Use the SDK to acquire a lock, ensuring only one instance runs a critical task at a time.
import { EdgeLock } from "@edgelock/client";// Ensure EdgeLock is configured (see Step 3)// EdgeLock.configure({ apiKey: process.env.EDGELOCK_KEY });async function runCriticalTask() {const lockKey = "my-unique-task";let lock = null;try {lock = await EdgeLock.acquire(lockKey, {ttl: 60, // TTL in seconds// timeout: 5000 // Optional: wait 5s for the lock});if (lock) {try {console.log(`Lock acquired (ID: ${lock.lockId})! Running critical task...`);await new Promise(resolve => setTimeout(resolve, 5000)); // Simulate workconsole.log("Task finished.");} finally {await lock.release();console.log("Lock released.");}} else {console.log(`Failed to acquire lock "${lockKey}". Another instance may hold it.`);}} catch (error) {console.error("An error occurred:", error);// Handle specific EdgeLockError if needed}}runCriticalTask();
Congratulations! You've acquired and released your first lock with EdgeLock. Explore the rest of the documentation to learn more about advanced features, configuration options, and best practices: