Cast Actions
Cast Actions let developers create custom buttons which users can install into their action bar on any Farcaster application (see the spec).
Overview
At a glance:
- User installs Cast Action via specific deeplink or by clicking on
<Button.AddCastAction>element with a specified target.castActionroute in a Frame. - When the user presses the Cast Action button in the App, the App will make a
POSTrequest to the.castActionroute. - Frame performs any action and returns a response to the App.
Walkthrough
Here is a trivial example on how to expose an action with a frame. We will break it down below.
1. Render Frame & Add Action Intent
In the example above, we are rendering Add Action intent:
actionproperty is used to set the path to the cast action route.nameproperty is used to set the name of the action. It must be less than 30 charactersiconproperty is used to associate your Cast Action with one of the Octicons. You can see the supported list here.
src/index.tsx
app.frame('/', (c) => {
return c.res({
image: (
<div style={{ color: 'white', display: 'flex', fontSize: 60 }}>
Add "Log this!" Action
</div>
),
intents: [
<Button.AddCastAction
action="/log-this"
name="Log This!"
icon="log"
>
Add
</Button.AddCastAction>,
]
})
})
// ...2. Handle /log-this Requests
Without a route handler to handle the Action request, the Cast Action will be meaningless.
Thus, let's define a /log-this route to handle the the Cast Action:
src/index.tsx
app.frame('/', (c) => {
return c.res({
image: (
<div style={{ color: 'white', display: 'flex', fontSize: 60 }}>
Add "Log this!" Action
</div>
),
intents: [
<Button.AddCastAction
action="/log-this"
name="Log This!"
icon="log"
>
Add
</Button.AddCastAction>,
]
})
})
app.castAction('/log-this', (c) => {
console.log(
`Cast Action to ${JSON.stringify(c.actionData.castId)} from ${
c.actionData.fid
}`,
)
return c.res({ message: 'Action Succeeded' })
})A breakdown of the /log-this route handler:
c.actionDatais never nullable and is always defined since Cast Actions always doPOSTrequest.- We are responding with a
c.resresponse and specifying amessagethat will appear in the success toast.
:::
5. Bonus: Learn the API
You can learn more about the transaction APIs here: