Actor quickstart
In this guide you create an actor that pushes a few items to its dataset, run it locally, then push it to Lantfly and run it from the CLI and the API.
You need:
- Node.js 22 or newer (for the Node.js actor and the CLI), or Python 3.10 or newer for a Python actor.
- The Lantfly CLI, logged in with
lantfly login --url https://lantfly.com. - Docker, only if you build the image yourself.
@lantfly/cli, @lantfly/actor-sdk and the Python package lantfly are not published to npm and PyPI yet. The steps below show how they work once they are. Until then npm install and pip install of these packages fail.
1. Scaffold the actor
- Node.js
- Python
lantfly init my-actor
cd my-actor
lantfly init creates:
my-actor/
.actor/
actor.json # name, title, version, schemas, Dockerfile
input_schema.json # the input form and validation
dataset_schema.json # how the Console shows the results
src/main.js # your code
storage/key_value_stores/default/INPUT.json # input for local runs
Dockerfile
.dockerignore
.gitignore
package.json # depends on @lantfly/actor-sdk
README.md
The code reads the input and pushes count items:
import { Actor } from '@lantfly/actor-sdk';
await Actor.main(async () => {
const { message = 'Hello from Lantfly', count = 3 } = await Actor.getInput();
Actor.log.info(`Pushing ${count} items`);
for (let index = 1; index <= count; index++) {
await Actor.pushData({ index, message, at: new Date().toISOString() });
}
await Actor.setValue('OUTPUT', { message, count });
});
The CLI does not scaffold Python actors yet. Create the files yourself:
my-actor/
.actor/
actor.json
input_schema.json
dataset_schema.json
main.py
requirements.txt
Dockerfile
.dockerignore # storage, .venv, __pycache__
import asyncio
from datetime import datetime, timezone
from lantfly import Actor
async def main() -> None:
async with Actor:
actor_input = await Actor.get_input()
message = actor_input.get("message", "Hello from Lantfly")
count = actor_input.get("count", 3)
Actor.log.info(f"Pushing {count} items")
for index in range(1, count + 1):
await Actor.push_data({"index": index, "message": message, "at": datetime.now(timezone.utc).isoformat()})
await Actor.set_value("OUTPUT", {"message": message, "count": count})
asyncio.run(main())
lantfly>=0.2,<1
FROM python:3.12-slim-bookworm
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 PIP_NO_CACHE_DIR=1
RUN apt-get update \
&& apt-get install -y --no-install-recommends tini ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --uid 1000 --create-home --shell /usr/sbin/nologin actor
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
USER 1000:1000
ENTRYPOINT ["tini", "--"]
CMD ["python", "main.py"]
Use the same .actor/ files as the Node.js tab (Actor definition shows them), with "dockerfile": "../Dockerfile".
2. Run it locally
- Node.js
- Python
npm install
npm start
python -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
mkdir -p storage/key_value_stores/default
echo '{"message": "Hello from my machine", "count": 2}' > storage/key_value_stores/default/INPUT.json
python main.py
Without LANTFLY_IS_AT_HOME, the SDK runs in local mode: it reads storage/key_value_stores/default/INPUT.json and writes one file per item to storage/datasets/default/ and the OUTPUT record to storage/key_value_stores/default/OUTPUT.json.
cat storage/datasets/default/000000001.json
Delete storage/datasets/default/ between local runs: local datasets keep appending.
3. Push it to Lantfly
lantfly push
push reads .actor/actor.json, creates the actor under your account (or updates it), creates version 0.1, uploads the folder and streams the build log. When the build succeeds, the version points at the new image.
If you prefer to build from a public Git repository, run lantfly push --git-url https://github.com/you/my-actor. If builds are not available for your account, push prints the docker build and docker push commands to run yourself, then lantfly push --image-tag <tag>. See Builds and versions.
4. Run it on Lantfly
From the CLI:
lantfly call <username>~my-actor --input '{"count": 5}'
The CLI streams the log and prints the status and the number of items. Your username is shown by lantfly info.
From the API:
curl -X POST "https://lantfly.com/api/v3/actors/<username>~my-actor/runs" \
-H "Authorization: Bearer $LANTFLY_TOKEN" \
-H "Content-Type: application/json" \
-d '{"input": {"count": 5}, "wait_for_finish": 60}'
curl "https://lantfly.com/api/v3/datasets/<default_dataset_id>/items?format=json&clean=1" \
-H "Authorization: Bearer $LANTFLY_TOKEN"
In the Console (https://lantfly.com), the actor is under My actors. Open it to start a run from the form built from your input schema, and to see the log, the dataset and the key-value store of each run.
Next steps
- Describe your input properly: Input schema.
- Show results as a table in the Console: Actor definition.
- Use the proxy and the crawler helpers: Actor SDK for Node.js.
- Run it on a schedule or get a webhook when it finishes: Webhooks.