Skip to content

Commit 79d2942

Browse files
committed
feat(sdk,core): add envvars.bulkDelete() and document the endpoint
Adds `envvars.bulkDelete(projectRef, slug, { keys, onlyWrittenBy?, onlyShadowingParent? })`, resolving projectRef and slug from the task context like the neighbouring envvars functions, plus the matching `bulkDeleteEnvVars` API client method and param type. Documents the endpoint in the management API reference and caps each key at 256 characters in the request schema.
1 parent 759240c commit 79d2942

9 files changed

Lines changed: 243 additions & 5 deletions

File tree

.changeset/envvar-bulk-delete.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
---
2+
"@trigger.dev/sdk": patch
23
"@trigger.dev/core": patch
34
---
45

5-
Add a bulk delete endpoint for environment variables, `POST /api/v1/projects/:projectRef/envvars/:slug/bulk-delete`, which removes up to 1000 variables from one environment in a single call and can be limited to values last written by a given source or to branch values that shadow a value on the parent environment.
6+
Delete many environment variables in one call with `envvars.bulkDelete()`, backed by the new `POST /api/v1/projects/:projectRef/envvars/:slug/bulk-delete` endpoint. You can limit the delete to values last written by a given source, or to branch values that shadow a value on the parent environment, and the response lists the keys that were deleted and the keys that were skipped.
7+
8+
```ts
9+
import { envvars } from "@trigger.dev/sdk";
10+
11+
const result = await envvars.bulkDelete("proj_yubjwjsfkxnylobaqvqz", "dev", {
12+
keys: ["SLACK_API_KEY", "STRIPE_SECRET_KEY"],
13+
});
14+
```

docs/docs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@
410410
"management/envvars/create",
411411
"management/envvars/retrieve",
412412
"management/envvars/update",
413-
"management/envvars/delete"
413+
"management/envvars/delete",
414+
"management/envvars/bulk-delete"
414415
]
415416
},
416417
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: "Bulk delete env vars"
3+
openapi: "v3-openapi POST /api/v1/projects/{projectRef}/envvars/{env}/bulk-delete"
4+
---

docs/v3-openapi.yaml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,124 @@ paths:
21852185
override: false
21862186
});
21872187
2188+
"/api/v1/projects/{projectRef}/envvars/{env}/bulk-delete":
2189+
parameters:
2190+
- $ref: "#/components/parameters/projectRef"
2191+
- $ref: "#/components/parameters/env"
2192+
post:
2193+
operationId: bulk_delete_project_envvars_v1
2194+
summary: Bulk delete environment variables
2195+
description: Delete up to 1000 environment variables from a specific project and environment in one request. Keys that have no value in the environment, or that the filters exclude, are returned as skipped.
2196+
requestBody:
2197+
required: true
2198+
content:
2199+
application/json:
2200+
schema:
2201+
type: object
2202+
properties:
2203+
keys:
2204+
type: array
2205+
description: The names of the variables to delete from this environment.
2206+
minItems: 1
2207+
maxItems: 1000
2208+
items:
2209+
type: string
2210+
minLength: 1
2211+
maxLength: 256
2212+
onlyWrittenBy:
2213+
description: Only delete values that were last written by this source.
2214+
oneOf:
2215+
- type: object
2216+
properties:
2217+
type:
2218+
type: string
2219+
enum: ["user"]
2220+
userId:
2221+
type: string
2222+
required: ["type", "userId"]
2223+
- type: object
2224+
properties:
2225+
type:
2226+
type: string
2227+
enum: ["integration"]
2228+
integration:
2229+
type: string
2230+
required: ["type", "integration"]
2231+
onlyShadowingParent:
2232+
type: boolean
2233+
description: On a preview branch, only delete values whose key also has a value on the parent environment, so the branch falls back to the shared value.
2234+
default: false
2235+
required: ["keys"]
2236+
responses:
2237+
"200":
2238+
description: The request completed. Each requested key appears in either `deleted` or `skipped`.
2239+
content:
2240+
application/json:
2241+
schema:
2242+
type: object
2243+
properties:
2244+
deleted:
2245+
type: array
2246+
description: Keys whose value was removed from the environment.
2247+
items:
2248+
type: string
2249+
skipped:
2250+
type: array
2251+
description: Keys that were left untouched, because the environment had no value for them or a filter excluded them.
2252+
items:
2253+
type: string
2254+
required: ["deleted", "skipped"]
2255+
"400":
2256+
description: Invalid request parameters or body
2257+
content:
2258+
application/json:
2259+
schema:
2260+
"$ref": "#/components/schemas/ErrorResponse"
2261+
"401":
2262+
description: Unauthorized request
2263+
content:
2264+
application/json:
2265+
schema:
2266+
"$ref": "#/components/schemas/ErrorResponse"
2267+
"404":
2268+
description: Resource not found
2269+
content:
2270+
application/json:
2271+
schema:
2272+
"$ref": "#/components/schemas/ErrorResponse"
2273+
tags:
2274+
- envvars
2275+
security:
2276+
- secretKey: []
2277+
- personalAccessToken: []
2278+
x-codeSamples:
2279+
- lang: typescript
2280+
label: Outside of a task
2281+
source: |-
2282+
import { envvars } from "@trigger.dev/sdk";
2283+
2284+
const result = await envvars.bulkDelete("proj_yubjwjsfkxnylobaqvqz", "dev", {
2285+
keys: ["SLACK_API_KEY", "STRIPE_SECRET_KEY"],
2286+
});
2287+
2288+
console.log(result.deleted, result.skipped);
2289+
- lang: typescript
2290+
label: Inside a task
2291+
source: |-
2292+
import { envvars, task } from "@trigger.dev/sdk";
2293+
2294+
export const myTask = task({
2295+
id: "my-task",
2296+
run: async () => {
2297+
// projectRef and env are automatically inferred from the task context
2298+
const result = await envvars.bulkDelete({
2299+
keys: ["SLACK_API_KEY", "STRIPE_SECRET_KEY"],
2300+
});
2301+
2302+
console.log(result.deleted, result.skipped);
2303+
}
2304+
})
2305+
21882306
"/api/v1/projects/{projectRef}/envvars/{env}/{name}":
21892307
parameters:
21902308
- $ref: "#/components/parameters/projectRef"

packages/core/src/v3/apiClient/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
CreateUploadPayloadUrlResponseBody,
5454
SessionTranscriptResponseBody,
5555
CreateWaitpointTokenResponseBody,
56+
BulkDeleteEnvironmentVariablesResponseBody,
5657
CreatedSessionResponseBody,
5758
DeletedScheduleObject,
5859
EndAndContinueSessionResponseBody,
@@ -129,6 +130,7 @@ import {
129130
STREAM_START_HEADER,
130131
} from "./runStream.js";
131132
import type {
133+
BulkDeleteEnvironmentVariablesParams,
132134
CreateBulkActionOptions,
133135
CreateEnvironmentVariableParams,
134136
ImportEnvironmentVariablesParams,
@@ -154,6 +156,7 @@ export type CreateBatchApiResponse = Prettify<
154156
>;
155157

156158
export type {
159+
BulkDeleteEnvironmentVariablesParams,
157160
CreateBulkActionOptions,
158161
CreateEnvironmentVariableParams,
159162
ImportEnvironmentVariablesParams,
@@ -1187,6 +1190,24 @@ export class ApiClient {
11871190
);
11881191
}
11891192

1193+
bulkDeleteEnvVars(
1194+
projectRef: string,
1195+
slug: string,
1196+
body: BulkDeleteEnvironmentVariablesParams,
1197+
requestOptions?: ZodFetchOptions
1198+
) {
1199+
return zodfetch(
1200+
BulkDeleteEnvironmentVariablesResponseBody,
1201+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/bulk-delete`,
1202+
{
1203+
method: "POST",
1204+
headers: this.#getHeaders(false),
1205+
body: JSON.stringify(body),
1206+
},
1207+
mergeRequestOptions(this.defaultRequestOptions, requestOptions)
1208+
);
1209+
}
1210+
11901211
updateRunMetadata(
11911212
runId: string,
11921213
body: UpdateMetadataRequestBody,

packages/core/src/v3/apiClient/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
EnvironmentVariableSource,
23
MachinePresetName,
34
QueueTypeName,
45
RunStatus,
@@ -29,6 +30,15 @@ export interface UpdateEnvironmentVariableParams {
2930
value: string;
3031
}
3132

33+
export type BulkDeleteEnvironmentVariablesParams = {
34+
/** The variables to delete from the environment, up to 1000 per call. */
35+
keys: string[];
36+
/** Only delete values that were last written by this source. */
37+
onlyWrittenBy?: EnvironmentVariableSource;
38+
/** Only delete values on a branch whose key also has a value on the parent environment. */
39+
onlyShadowingParent?: boolean;
40+
};
41+
3242
export interface ListRunsQueryParams extends CursorPageParams {
3343
status?: Array<RunStatus> | RunStatus;
3444
taskIdentifier?: Array<string> | string;

packages/core/src/v3/schemas/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ export type ImportEnvironmentVariablesRequestBody = z.infer<
16231623
>;
16241624

16251625
export const BulkDeleteEnvironmentVariablesRequestBody = z.object({
1626-
keys: z.array(z.string()).min(1).max(1000),
1626+
keys: z.array(z.string().min(1).max(256)).min(1).max(1000),
16271627
/** Only remove values last written by this source. */
16281628
onlyWrittenBy: EnvironmentVariableSource.optional(),
16291629
/** Only remove values whose key also has a value on the parent environment. */

packages/trigger-sdk/src/v3/envvars.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type {
22
ApiPromise,
33
ApiRequestOptions,
4+
BulkDeleteEnvironmentVariablesParams,
5+
BulkDeleteEnvironmentVariablesResponseBody,
46
CreateEnvironmentVariableParams,
57
EnvironmentVariableResponseBody,
68
EnvironmentVariableWithSecret,
@@ -15,7 +17,11 @@ import {
1517
} from "@trigger.dev/core/v3";
1618
import { tracer } from "./tracer.js";
1719

18-
export type { CreateEnvironmentVariableParams, ImportEnvironmentVariablesParams };
20+
export type {
21+
BulkDeleteEnvironmentVariablesParams,
22+
CreateEnvironmentVariableParams,
23+
ImportEnvironmentVariablesParams,
24+
};
1925

2026
export function upload(
2127
projectRef: string,
@@ -278,6 +284,72 @@ export function del(
278284
return apiClient.deleteEnvVar($projectRef, $slug, $name, $requestOptions);
279285
}
280286

287+
export function bulkDelete(
288+
projectRef: string,
289+
slug: string,
290+
params: BulkDeleteEnvironmentVariablesParams,
291+
requestOptions?: ApiRequestOptions
292+
): ApiPromise<BulkDeleteEnvironmentVariablesResponseBody>;
293+
export function bulkDelete(
294+
params: BulkDeleteEnvironmentVariablesParams,
295+
requestOptions?: ApiRequestOptions
296+
): ApiPromise<BulkDeleteEnvironmentVariablesResponseBody>;
297+
export function bulkDelete(
298+
projectRefOrParams: string | BulkDeleteEnvironmentVariablesParams,
299+
slugOrRequestOptions?: string | ApiRequestOptions,
300+
params?: BulkDeleteEnvironmentVariablesParams,
301+
requestOptions?: ApiRequestOptions
302+
): ApiPromise<BulkDeleteEnvironmentVariablesResponseBody> {
303+
let $projectRef: string;
304+
let $params: BulkDeleteEnvironmentVariablesParams;
305+
let $slug: string;
306+
const $requestOptions = overloadRequestOptions(
307+
"bulkDelete",
308+
slugOrRequestOptions,
309+
requestOptions
310+
);
311+
312+
if (taskContext.ctx) {
313+
if (typeof projectRefOrParams === "string") {
314+
$projectRef = projectRefOrParams;
315+
$slug =
316+
typeof slugOrRequestOptions === "string"
317+
? slugOrRequestOptions
318+
: taskContext.ctx.environment.slug;
319+
320+
if (!params) {
321+
throw new Error("params is required");
322+
}
323+
324+
$params = params;
325+
} else {
326+
$params = projectRefOrParams;
327+
$projectRef = taskContext.ctx.project.ref;
328+
$slug = taskContext.ctx.environment.slug;
329+
}
330+
} else {
331+
if (typeof projectRefOrParams !== "string") {
332+
throw new Error("projectRef is required");
333+
}
334+
335+
if (!slugOrRequestOptions || typeof slugOrRequestOptions !== "string") {
336+
throw new Error("slug is required");
337+
}
338+
339+
if (!params) {
340+
throw new Error("params is required");
341+
}
342+
343+
$projectRef = projectRefOrParams;
344+
$slug = slugOrRequestOptions;
345+
$params = params;
346+
}
347+
348+
const apiClient = apiClientManager.clientOrThrow();
349+
350+
return apiClient.bulkDeleteEnvVars($projectRef, $slug, $params, $requestOptions);
351+
}
352+
281353
export function update(
282354
projectRef: string,
283355
slug: string,

packages/trigger-sdk/src/v3/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export {
6565
} from "./deployments.js";
6666
export * as envvars from "./envvars.js";
6767
export * as queues from "./queues.js";
68-
export type { ImportEnvironmentVariablesParams } from "./envvars.js";
68+
export type {
69+
BulkDeleteEnvironmentVariablesParams,
70+
ImportEnvironmentVariablesParams,
71+
} from "./envvars.js";
6972

7073
export { configure, auth } from "./auth.js";
7174
export { TriggerClient, type TriggerClientConfig } from "./triggerClient.js";

0 commit comments

Comments
 (0)