|  | 
|  | 1 | +--- | 
|  | 2 | +title: "Context" | 
|  | 3 | +description: "Get the context of a task run." | 
|  | 4 | +--- | 
|  | 5 | + | 
|  | 6 | +Context (`ctx`) is a way to get information about a run.  | 
|  | 7 | + | 
|  | 8 | +<Note> | 
|  | 9 | +The context object does not change whilst your code is executing. This means values like `ctx.run.durationMs` will be fixed at the moment the `run()` function is called. | 
|  | 10 | +</Note> | 
|  | 11 | + | 
|  | 12 | +Here's an example: | 
|  | 13 | + | 
|  | 14 | +```typescript | 
|  | 15 | +import { task } from "@trigger.dev/sdk/v3"; | 
|  | 16 | + | 
|  | 17 | +export const parentTask = task({ | 
|  | 18 | +  id: "parent-task", | 
|  | 19 | +  run: async (payload: { message: string }, { ctx }) => { | 
|  | 20 | +     | 
|  | 21 | +    if (ctx.environment.type === "DEVELOPMENT") { | 
|  | 22 | +      return; | 
|  | 23 | +    } | 
|  | 24 | +  }, | 
|  | 25 | +}); | 
|  | 26 | +``` | 
|  | 27 | + | 
|  | 28 | +## Context properties | 
|  | 29 | + | 
|  | 30 | +<ResponseField name="task" type="object"> | 
|  | 31 | +  <Expandable title="properties" defaultOpen={true}> | 
|  | 32 | +    <ResponseField name="exportName" type="string"> | 
|  | 33 | +      The exported function name of the task e.g. `myTask` if you defined it like this: `export const myTask = task(...)`. | 
|  | 34 | +    </ResponseField> | 
|  | 35 | +    <ResponseField name="id" type="string"> | 
|  | 36 | +      The ID of the task. | 
|  | 37 | +    </ResponseField> | 
|  | 38 | +    <ResponseField name="filePath" type="string"> | 
|  | 39 | +      The file path of the task. | 
|  | 40 | +    </ResponseField> | 
|  | 41 | +  </Expandable> | 
|  | 42 | +</ResponseField> | 
|  | 43 | + | 
|  | 44 | +<ResponseField name="attempt" type="object"> | 
|  | 45 | +  <Expandable title="properties"> | 
|  | 46 | +    <ResponseField name="id" type="string"> | 
|  | 47 | +      The ID of the execution attempt. | 
|  | 48 | +    </ResponseField> | 
|  | 49 | +    <ResponseField name="number" type="number"> | 
|  | 50 | +      The attempt number. | 
|  | 51 | +    </ResponseField> | 
|  | 52 | +    <ResponseField name="startedAt" type="date"> | 
|  | 53 | +      The start time of the attempt. | 
|  | 54 | +    </ResponseField> | 
|  | 55 | +    <ResponseField name="backgroundWorkerId" type="string"> | 
|  | 56 | +      The ID of the background worker. | 
|  | 57 | +    </ResponseField> | 
|  | 58 | +    <ResponseField name="backgroundWorkerTaskId" type="string"> | 
|  | 59 | +      The ID of the background worker task. | 
|  | 60 | +    </ResponseField> | 
|  | 61 | +    <ResponseField name="status" type="string"> | 
|  | 62 | +      The current status of the attempt. | 
|  | 63 | +    </ResponseField> | 
|  | 64 | +  </Expandable> | 
|  | 65 | +</ResponseField> | 
|  | 66 | + | 
|  | 67 | +<ResponseField name="run" type="object"> | 
|  | 68 | +  <Expandable title="properties"> | 
|  | 69 | +    <ResponseField name="id" type="string"> | 
|  | 70 | +      The ID of the task run. | 
|  | 71 | +    </ResponseField> | 
|  | 72 | +    <ResponseField name="context" type="any" optional> | 
|  | 73 | +      The context of the task run. | 
|  | 74 | +    </ResponseField> | 
|  | 75 | +    <ResponseField name="tags" type="array"> | 
|  | 76 | +      An array of [tags](/tags) associated with the task run. | 
|  | 77 | +    </ResponseField> | 
|  | 78 | +    <ResponseField name="isTest" type="boolean"> | 
|  | 79 | +      Whether this is a [test run](/run-tests). | 
|  | 80 | +    </ResponseField> | 
|  | 81 | +    <ResponseField name="createdAt" type="date"> | 
|  | 82 | +      The creation time of the task run. | 
|  | 83 | +    </ResponseField> | 
|  | 84 | +    <ResponseField name="startedAt" type="date"> | 
|  | 85 | +      The start time of the task run. | 
|  | 86 | +    </ResponseField> | 
|  | 87 | +    <ResponseField name="idempotencyKey" type="string" optional> | 
|  | 88 | +      An optional [idempotency key](/idempotency) for the task run. | 
|  | 89 | +    </ResponseField> | 
|  | 90 | +    <ResponseField name="maxAttempts" type="number" optional> | 
|  | 91 | +      The [maximum number of attempts](/triggering#maxattempts) allowed for this task run. | 
|  | 92 | +    </ResponseField> | 
|  | 93 | +    <ResponseField name="durationMs" type="number"> | 
|  | 94 | +      The duration of the task run in milliseconds when the `run()` function is called. For live values use the [usage SDK functions](/run-usage). | 
|  | 95 | +    </ResponseField> | 
|  | 96 | +    <ResponseField name="costInCents" type="number"> | 
|  | 97 | +      The cost of the task run in cents when the `run()` function is called. For live values use the [usage SDK functions](/run-usage). | 
|  | 98 | +    </ResponseField> | 
|  | 99 | +    <ResponseField name="baseCostInCents" type="number"> | 
|  | 100 | +      The base cost of the task run in cents when the `run()` function is called. For live values use the [usage SDK functions](/run-usage). | 
|  | 101 | +    </ResponseField> | 
|  | 102 | +    <ResponseField name="version" type="string" optional> | 
|  | 103 | +      The [version](/versioning) of the task run. | 
|  | 104 | +    </ResponseField> | 
|  | 105 | +    <ResponseField name="maxDuration" type="number" optional> | 
|  | 106 | +      The [maximum allowed duration](/runs/max-duration) for the task run. | 
|  | 107 | +    </ResponseField> | 
|  | 108 | +  </Expandable> | 
|  | 109 | +</ResponseField> | 
|  | 110 | + | 
|  | 111 | +<ResponseField name="queue" type="object"> | 
|  | 112 | +  <Expandable title="properties"> | 
|  | 113 | +    <ResponseField name="id" type="string"> | 
|  | 114 | +      The ID of the queue. | 
|  | 115 | +    </ResponseField> | 
|  | 116 | +    <ResponseField name="name" type="string"> | 
|  | 117 | +      The name of the queue. | 
|  | 118 | +    </ResponseField> | 
|  | 119 | +  </Expandable> | 
|  | 120 | +</ResponseField> | 
|  | 121 | + | 
|  | 122 | +<ResponseField name="environment" type="object"> | 
|  | 123 | +  <Expandable title="properties"> | 
|  | 124 | +    <ResponseField name="id" type="string"> | 
|  | 125 | +      The ID of the environment. | 
|  | 126 | +    </ResponseField> | 
|  | 127 | +    <ResponseField name="slug" type="string"> | 
|  | 128 | +      The slug of the environment. | 
|  | 129 | +    </ResponseField> | 
|  | 130 | +    <ResponseField name="type" type="string"> | 
|  | 131 | +      The type of the environment (PRODUCTION, STAGING, DEVELOPMENT, or PREVIEW). | 
|  | 132 | +    </ResponseField> | 
|  | 133 | +  </Expandable> | 
|  | 134 | +</ResponseField> | 
|  | 135 | + | 
|  | 136 | +<ResponseField name="organization" type="object"> | 
|  | 137 | +  <Expandable title="properties"> | 
|  | 138 | +    <ResponseField name="id" type="string"> | 
|  | 139 | +      The ID of the organization. | 
|  | 140 | +    </ResponseField> | 
|  | 141 | +    <ResponseField name="slug" type="string"> | 
|  | 142 | +      The slug of the organization. | 
|  | 143 | +    </ResponseField> | 
|  | 144 | +    <ResponseField name="name" type="string"> | 
|  | 145 | +      The name of the organization. | 
|  | 146 | +    </ResponseField> | 
|  | 147 | +  </Expandable> | 
|  | 148 | +</ResponseField> | 
|  | 149 | + | 
|  | 150 | +<ResponseField name="project" type="object"> | 
|  | 151 | +  <Expandable title="properties"> | 
|  | 152 | +    <ResponseField name="id" type="string"> | 
|  | 153 | +      The ID of the project. | 
|  | 154 | +    </ResponseField> | 
|  | 155 | +    <ResponseField name="ref" type="string"> | 
|  | 156 | +      The reference of the project. | 
|  | 157 | +    </ResponseField> | 
|  | 158 | +    <ResponseField name="slug" type="string"> | 
|  | 159 | +      The slug of the project. | 
|  | 160 | +    </ResponseField> | 
|  | 161 | +    <ResponseField name="name" type="string"> | 
|  | 162 | +      The name of the project. | 
|  | 163 | +    </ResponseField> | 
|  | 164 | +  </Expandable> | 
|  | 165 | +</ResponseField> | 
|  | 166 | + | 
|  | 167 | +<ResponseField name="batch" type="object" optional> | 
|  | 168 | +  Optional information about the batch, if applicable. | 
|  | 169 | +  <Expandable title="properties"> | 
|  | 170 | +    <ResponseField name="id" type="string"> | 
|  | 171 | +      The ID of the batch. | 
|  | 172 | +    </ResponseField> | 
|  | 173 | +  </Expandable> | 
|  | 174 | +</ResponseField> | 
|  | 175 | + | 
|  | 176 | +<ResponseField name="machine" type="object" optional> | 
|  | 177 | +  Optional information about the machine preset used for execution. | 
|  | 178 | +  <Expandable title="properties"> | 
|  | 179 | +    <ResponseField name="name" type="string"> | 
|  | 180 | +      The name of the machine preset. | 
|  | 181 | +    </ResponseField> | 
|  | 182 | +    <ResponseField name="cpu" type="number"> | 
|  | 183 | +      The CPU allocation for the machine. | 
|  | 184 | +    </ResponseField> | 
|  | 185 | +    <ResponseField name="memory" type="number"> | 
|  | 186 | +      The memory allocation for the machine. | 
|  | 187 | +    </ResponseField> | 
|  | 188 | +    <ResponseField name="centsPerMs" type="number"> | 
|  | 189 | +      The cost in cents per millisecond for this machine preset. | 
|  | 190 | +    </ResponseField> | 
|  | 191 | +  </Expandable> | 
|  | 192 | +</ResponseField> | 
0 commit comments