Getting Started

Quickstart

Get started with EdgeLock in just a few minutes.

1. Sign Up & Get API Key

First, you'll need an EdgeLock account and an API key.

  1. Sign up at app.edgelock.dev.
  2. Navigate to the API Keys section in your dashboard.
  3. Generate a new API key and copy it securely.

2. Install SDK

Install the JavaScript/TypeScript SDK using your preferred package manager.

npm install @edgelock/client
# or
yarn add @edgelock/client
# or
bun add @edgelock/client

3. Configure Your Environment

Set your API key as an environment variable for the SDK.

Terminal (Bash/Zsh)
export EDGELOCK_KEY="your_api_key_here"

Alternatively, configure the client directly with the key (less recommended for production):

your-app.ts
import { EdgeLock } from "@edgelock/client";
// Configure the client once
EdgeLock.configure({
apiKey: process.env.EDGELOCK_KEY || "your_api_key_here",
});

4. Acquire Your First Lock

Use the SDK to acquire a lock, ensuring only one instance runs a critical task at a time.

your-app.ts
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 work
console.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();

Next Steps

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: