> ## Documentation Index
> Fetch the complete documentation index at: https://www.kernel.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Invoke an App

> Run an app action from the API or CLI, pass a payload, and stop a running invocation

## Via API

You can invoke your app by making a `POST` request to Kernel's API or via the CLI. Both support passing a payload. **For automations and agents that take longer than 100 seconds, use [async invocations](https://www.kernel.sh/docs/apps/invoke.md#asynchronous-invocations).**

> [!NOTE]
>
> Synchronous invocations time out after 100 seconds.

```typescript Typescript/Javascript theme={null}
import Kernel from '@onkernel/sdk';

const kernel = new Kernel();

const invocation = await kernel.invocations.create({
  action_name: 'analyze',
  app_name: 'my-app',
  version: '1.0.0',
});

console.log(invocation.id);
```

```python Python theme={null}
from kernel import Kernel

kernel = Kernel()
invocation = kernel.invocations.create(
    action_name="analyze",
    app_name="my-app",
    version="1.0.0",
)
print(invocation.id)
```

```go Go theme={null}
package main

import (
	"context"
	"fmt"

	"github.com/kernel/kernel-go-sdk"
)

func main() {
	ctx := context.Background()
	client := kernel.NewClient()

	invocation, err := client.Invocations.New(ctx, kernel.InvocationNewParams{
		ActionName: "analyze",
		AppName:    "my-app",
		Version:    "1.0.0",
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(invocation.ID)
}
```

### Asynchronous invocations

For long running jobs, use asynchronous invocations to trigger Kernel actions without waiting for the result. You can then stream real-time [status updates](https://www.kernel.sh/docs/apps/status.md#streaming-status-updates) for the result.

> [!NOTE]
>
> Asynchronous invocations time out after 15 minutes by default but can be configured to last up to 1 hour by setting the optional `async_timeout_seconds` parameter during invocation.

```typescript Typescript/Javascript theme={null}
import Kernel from '@onkernel/sdk';

const kernel = new Kernel();

const invocation = await kernel.invocations.create({
  async: true,
  action_name: 'analyze',
  app_name: 'my-app',
  version: '1.0.0',
});

console.log(invocation.id);
```

```python Python theme={null}
from kernel import Kernel

kernel = Kernel()
invocation = kernel.invocations.create(
    action_name="analyze",
    app_name="my-app",
    version="1.0.0",
    async_=True,
)
print(invocation.id)
```

```go Go theme={null}
package main

import (
	"context"
	"fmt"

	"github.com/kernel/kernel-go-sdk"
)

func main() {
	ctx := context.Background()
	client := kernel.NewClient()

	invocation, err := client.Invocations.New(ctx, kernel.InvocationNewParams{
		Async:      kernel.Bool(true),
		ActionName: "analyze",
		AppName:    "my-app",
		Version:    "1.0.0",
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(invocation.ID)
}
```

## Via CLI

Invoke an app action immediately via the CLI:

```bash theme={null}
kernel invoke <app_name> <action_name>
```

### Payload parameter

`--payload` allows you to invoke the action with specified parameters. This enables your action to receive and handle dynamic inputs at runtime. For example:

> [!NOTE]
>
> Payloads are stringified JSON and have a maximum size of 4.5 MB.

```bash theme={null}
kernel invoke <app_name> <action_name>
    --payload '{"tshirt_size": "small", "color": "black", "shipping_address": "2 Mint Plz, San Francisco CA 94103"}'
```

See [here](https://www.kernel.sh/docs/apps/develop.md#parameters) to learn how to access the payload in your action method.

### Return values

If your action specifies a [return value](https://www.kernel.sh/docs/apps/develop.md#return-values), the invocation returns its value once it completes. (The Kernel CLI uses asynchronous invocations under the hood)

## Stop an invocation

You can terminate an invocation that's running. This is useful for stopping automations or agents stuck in an infinite loop.

> [!NOTE]
>
> Terminating an invocation also destroys any browsers associated with it.

### Via API

You can stop an invocation by setting its status to `failed`. This will cancel the invocation and mark it as terminated.

```typescript Typescript/Javascript theme={null}
import Kernel from '@onkernel/sdk';

const kernel = new Kernel();

const invocation = await kernel.invocations.update('invocation_id', {
  status: 'failed',
  output: JSON.stringify({ error: 'Invocation cancelled by user' }),
});
```

```python Python theme={null}
from kernel import Kernel

kernel = Kernel()
invocation = kernel.invocations.update(
    id="invocation_id",
    status="failed",
    output='{"error":"Invocation cancelled by user"}',
)
```

```go Go theme={null}
package main

import (
	"context"

	"github.com/kernel/kernel-go-sdk"
)

func main() {
	ctx := context.Background()
	client := kernel.NewClient()

	invocation, err := client.Invocations.Update(ctx, "invocation_id", kernel.InvocationUpdateParams{
		Status: kernel.InvocationUpdateParamsStatusFailed,
		Output: kernel.String(`{"error":"Invocation cancelled by user"}`),
	})
	if err != nil {
		panic(err)
	}
	_ = invocation
}
```

### Via CLI

Use `ctrl-c` in the terminal tab where you launched the invocation.

App invocations accrue compute usage separately from any browsers they create. See the [pricing FAQ](https://www.kernel.sh/docs/info/pricing.md#faq) for how app invocations are charged.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.