DigitalOcean.Functions reference
AccessKey
Section titled “AccessKey”Source:
packages/digitalocean/src/Functions/AccessKey.ts
A key scoped to one Functions namespace. Send keyId:secret as HTTP basic
auth to the namespace’s apiHost to deploy or invoke its functions.
DigitalOcean returns the secret only at create, so Alchemy keeps it in
state as a Redacted value. InvokeFunction mints one per host.
AccessKey: Creating an Access Key
Section titled “AccessKey: Creating an Access Key”const key = yield* DigitalOcean.Functions.AccessKey("ci", { namespaceId: fns.namespaceId,});Action
Section titled “Action”Source:
packages/digitalocean/src/Functions/Action.ts
A function in a DigitalOcean Functions namespace (an OpenWhisk action).
Code, runtime, parameters and limits update in place; a new version is
deployed only when one of them changes. Set web: true to serve the
function at a public URL.
Action: Deploying a Function
Section titled “Action: Deploying a Function”const fns = yield* DigitalOcean.Functions.Namespace("fns", { region: "nyc1" });const hello = yield* DigitalOcean.Functions.Action("hello", { namespace: fns, runtime: "nodejs:18", web: true, code: `function main(args) { return { body: "hello " + (args.name ?? "world") } }`,});// hello.url → https://faas-nyc1-….doserverless.co/api/v1/web/fn-…/default/…Function
Section titled “Function”Source:
packages/digitalocean/src/Functions/Function.ts
A DigitalOcean Function running your program: an HTTPS endpoint that runs only when it’s called, billed per run. The program is bundled into one file and deployed as a web function in a Functions namespace.
Function: Writing a Function
Section titled “Function: Writing a Function”An Effect handler
export default class Hello extends DigitalOcean.Functions.Function<Hello>()( "Hello", { main: import.meta.url, namespace: Fns }, Effect.gen(function* () { return { fetch: Effect.gen(function* () { const request = yield* HttpServerRequest; return HttpServerResponse.text(`hello from ${request.url}`); }), }; }),) {}An async handler
export default { async fetch(request: Request) { return new Response("hello"); },};
// alchemy.run.tsconst hello = yield* DigitalOcean.Functions.Function("Hello", { main: "./src/hello.ts", namespace: fns, isExternal: true,});Function: Using Bindings
Section titled “Function: Using Bindings”export default class Api extends DigitalOcean.Functions.Function<Api>()( "Api", { main: import.meta.url, namespace: Fns }, Effect.gen(function* () { const stripe = yield* DigitalOcean.Security.ReadSecret(Stripe); return { fetch: Effect.gen(function* () { const key = yield* stripe.get("STRIPE_KEY"); return HttpServerResponse.text(Redacted.value(key).slice(0, 7)); }).pipe(Effect.orDie), }; }).pipe(Effect.provide(DigitalOcean.Security.ReadSecretHttp)),) {}InvokeFunction
Section titled “InvokeFunction”Source:
packages/digitalocean/src/Functions/InvokeFunction.ts
Invoke a DigitalOcean function from a runtime and wait for its result.
Binding it mints a Functions.AccessKey for the host, scoped to the
function’s namespace, because DigitalOcean signs no workload identity a
runtime could present instead; the key reaches the host as a secret.
InvokeFunction: Examples
Section titled “InvokeFunction: Examples”export default class Api extends DigitalOcean.App<Api>()( "Api", { main: import.meta.url }, Effect.gen(function* () { const fn = yield* Resize; const resize = yield* DigitalOcean.Functions.InvokeFunction(fn); return { fetch: Effect.gen(function* () { const out = yield* resize<{ url: string }>({ width: 200 }); return HttpServerResponse.text(out.url); }).pipe(Effect.orDie), }; }).pipe(Effect.provide(DigitalOcean.Functions.InvokeFunctionHttp)),) {}InvokeFunctionHttp
Section titled “InvokeFunctionHttp”Source:
packages/digitalocean/src/Functions/InvokeFunctionHttp.tsKind: Layer · Provides:DigitalOcean.Functions.InvokeFunction
InvokeFunction over the namespace’s OpenWhisk API, with a
Functions.AccessKey minted for the host.
Namespace
Section titled “Namespace”Source:
packages/digitalocean/src/Functions/Namespace.ts
A DigitalOcean Functions namespace: the place functions are deployed to
and served from. Each namespace has its own OpenWhisk API host. Deploy
code into it with Functions.Action and schedule it with
Functions.Trigger.
Namespace: Creating a Namespace
Section titled “Namespace: Creating a Namespace”const fns = yield* DigitalOcean.Functions.Namespace("fns", { region: "nyc1" });Trigger
Section titled “Trigger”Source:
packages/digitalocean/src/Functions/Trigger.ts
Runs a function on a cron schedule. The schedule, body and enabled
update in place.
Trigger: Scheduling a Function
Section titled “Trigger: Scheduling a Function”yield* DigitalOcean.Functions.Trigger("every-5m", { namespaceId: fns.namespaceId, function: cleanup.name, cron: "*\/5 * * * *", body: { dryRun: false },});