@layer-drone/protocol
v0.8.0
Published
Layer Drone protocol SDK with typed API client and event parsing
Keywords
Readme
LayerDrone Protocol SDK
Published as @layer-drone/protocol.
Description
This SDK provides a typed client for using LayerDrone's REST API and parsing webhook events.
Philosophy
The LayerDrone protocol exists as a hybrid of on and off-chain functionality. This helps strike the right balance of transparency, speed, and familiarity.
- Objects related to missions & reward entitlement are recorded on-chain
- Off-chain indexing supports non-critical data outside of smart contracts, keeping gas usage low
- A REST API and webhook broadcaster offer a familiar facade over aggregated on & off-chain data
Installation
$ npm install @layer-drone/protocolUsage
Getting an API key
Organizations and API keys are provisioned manually for private use only at the moment.
Making API calls
import * as LayerDrone from "@layer-drone/protocol";
const client = LayerDrone.createClient({
baseUrl: env.PROTOCOL_API_URL,
auth: env.PROTOCOL_API_KEY,
});
// Submit a flight for validation
const response = await LayerDrone.flightsControllerValidateFlight({
client,
body: {
storageKey: "s3://layerdrone-flights-staging/12345",
pilotAddress: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb1",
missionId: 123n,
flightTimestamp: "2023-10-15T14:25:30.123Z",
flightRequestInfo: {
flightPlanId: 2,
filePaths: [
"images/IMG_0001.JPG",
"images/IMG_0002.JPG",
"images/IMG_0003.JPG",
],
zoneHash: "https://explorer.spexi.com/zone/8928de8de43ffff",
provenancePath: "provenance.json",
},
},
});Registering webhooks
The SDK supports registering webhooks via the API.
import * as LayerDrone from "@layer-drone/protocol";
const client = LayerDrone.createClient({
baseUrl: process.env.PROTOCOL_API_URL!,
auth: process.env.PROTOCOL_API_KEY!,
});
const webhook = await LayerDrone.webhooksControllerCreate({
client,
path: { org_id: 1 },
body: {
name: "MyOrg Webhook",
url: "https://api.example.com/webhooks/layer-drone",
event_types: ["protocol.flight.reviewed"],
active: true,
},
});
// `webhook.id` is the config id. `webhook.secret` is the value LayerDrone
// will send on each request as the X-Webhook-Secret header.Receiving webhook events
Your API can parse webhook payloads at a registered endpoint.
import type { Request, Response } from "express";
import { parseWebhookEvent } from "@layer-drone/protocol";
class MyController {
async handleLayerDroneWebhookEvent(req: Request, res: Response) {
const event = parseWebhookEvent(req, process.env.LAYERDRONE_WEBHOOK_SECRET);
let message = "Event received";
switch (event.event_type) {
case "protocol.flight.reviewed":
// handle narrowed event type.
break;
default:
message = `Handler for ${event.event_type} not implemented`;
}
res.status(200).send({ message });
}
}