# Function Flow Overview

> How community functions move from creation to publication to consumption — Admin, CLI, CI, and Hub.

Community functions are reusable units of logic (TypeScript/JavaScript) that you can **create**, **publish** to the Aerostack registry, and **consume** in your projects. This page summarizes the lifecycle and where each tool fits.

## Lifecycle at a glance

```mermaid
flowchart LR
    subgraph create [Create]
        AdminCreate[Admin app]
        CLIPush[CLI functions push]
        CIPublish[CI ci/publish]
    end

    subgraph registry [Registry]
        Draft[draft]
        Pub[published]
        Draft --> Pub
    end

    subgraph consume [Consume]
        Hub[Hub browse]
        CLIInstall[CLI functions install]
    end

    AdminCreate --> Draft
    CLIPush --> Draft
    CIPublish --> Draft
    CIPublish --> Pub
    Pub --> Hub
    Pub --> CLIInstall
```

1. **Create** — Add a new function as a **draft** (Admin, CLI push, or CI).
2. **Publish** — Change status to **published** so it appears on the Hub and can be installed (Admin, CLI, or CI with `publish: true`).
3. **Consume** — Browse on the [Hub](https://hub.aerostack.dev) and install into your project with the CLI.

## Create via Admin or CLI

- **Admin**: Log in to the [Admin dashboard](https://app.aerostack.dev), go to **My Functions**, create a new function, and edit in the web editor (Monaco). Saves as draft.
- **CLI**: From your project, run `aerostack functions push [file]` (e.g. `aerostack functions push src/index.ts`). Each push **creates a new draft**; the command outputs the function ID and a link to the Admin to edit or publish. Use an `aerostack.json` in the same directory (or project root) for name, description, and category.

See [Publishing Overview](/hub/publishing-overview) and [Publishing via CLI](/hub/publish-cli) for details.

## Publish via Admin, CLI, or CI

- **Admin**: Open the function in the editor and click **Publish**. You can run an optional code validation first.
- **CLI**: After pushing, use the returned function ID: `aerostack functions publish <id>`.
- **CI**: Use `POST /api/community/functions/ci/publish` with your account API key. This is the **only path that upserts** by slug (create or update the same function). Set `publish: true` in the body to go live immediately.

See [Publishing Overview](/hub/publishing-overview), [Via CLI](/hub/publish-cli), [GitHub Actions](/hub/publish-github), and [GitLab CI](/hub/publish-gitlab).

## Consume via Hub and CLI

- **Hub**: Browse [hub.aerostack.dev](https://hub.aerostack.dev) for published functions. Open a function to see source, README, and install instructions.
- **CLI**: Install a function into your project with `aerostack functions install [username/slug]` or `aerostack functions install <slug>`. The CLI fetches the install manifest, writes files into `services/<slug>/`, and updates `aerostack.toml`.

See [Using Community Logic](/hub/consuming) and [Universal Adapter Pattern](/hub/architecture).

## Quick reference

| Goal | Where |
|------|--------|
| Create a draft | Admin (My Functions) or `aerostack functions push` |
| Update a draft | Admin (edit) or CI (`/ci/publish`); CLI push creates a new draft each time |
| Publish a draft | Admin (Publish) or `aerostack functions publish <id>` or CI with `publish: true` |
| Browse functions | [Hub](https://hub.aerostack.dev) |
| Install a function | `aerostack functions install [username/slug]` |
