Modern web apps often need more than just simple API endpoints. You need logic. Not just any logic, but stateful, resilient, and scalable logic โ think user onboarding flows, approval steps, reminders, and retries. Enter: Workflows.
And now, with Codehooks.io, building these workflows is easy, fast, and JavaScript-native. Let's dive in.
๐ง Why Workflows?
Whether youโre working on a side project or building enterprise-grade applications, workflows help you:
- Break complex logic into steps
- Add retries and error handling
- Pause and resume
- Scale reliably
๐งช A Simple Example: Odd or Even?
You'll need a codehooks.io account and a project before deploying your workflow code.
In this short tutorial we'll implement a simple workflow for deciding if a number is odd or even.
Letโs define a workflow in index.js:
import { app } from 'codehooks-js'
const workflow = app.createWorkflow('parityCheck', 'Check if a number is even or odd', {
  begin: async (state, goto) => {
    state.number = Math.floor(Math.random() * 100)
    goto('check', state)
  },
  check: async (state, goto) => {
    const step = (state.number % 2 === 0) ? 'even' : 'odd'
    goto(step, state) // branch
  },
  even: async (state, goto) => {
    console.log(`${state.number} is even`)
    goto('end', state)
  },
  odd: async (state, goto) => {
    console.log(`Number ${state.number} is odd`) 
    goto('end', state)
  },
  end: async (state, goto) => {
    console.log('Workflow finished', state)
    goto(null, state) // null complete the workflow
  }
})
// REST API to start a new workflow instance
app.post('/start', async (req, res) => {
  const result = await workflow.start({"Initial": "state"});
  res.json(result);
});
// export app interface to serverless runtime
export default app.init();
Deploy your workflow with the CLI command coho deploy or use the Codehooks Studio to work directly in the browser.
Calling the API endpoint POST https:{PROJECT_URL}/start will print the output from the workflow to the log:
dev 2025-06-15T07:03:05.714Z  Number 29 is odd
dev 2025-06-15T07:03:05.913Z  Workflow finished {
  "Initial": "state",
  "nextStep": "end",
  "createdAt": "2025-06-15T07:02:59.682Z",
  "workflowName": "parityCheck",
  "stepCount": {
    "begin": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:02.738Z",
      "totalTime": 6,
      "finishTime": "2025-06-15T07:03:02.744Z"
    },
    "check": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:05.006Z",
      "totalTime": 3,
      "finishTime": "2025-06-15T07:03:05.009Z"
    },
    "odd": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:05.709Z",
      "totalTime": 3,
      "finishTime": "2025-06-15T07:03:05.712Z"
    },
    "end": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:05.907Z",
      "totalTime": 0
    }
  },
  "_id": "684e7023878a78b8ac8338ab",
  "updatedAt": "2025-06-15T07:03:05.907Z",
  "number": 29,
  "previousStep": "odd",
  "instanceId": "684e7023878a78b8ac8338ab"
}
dev 2025-06-15T07:03:05.917Z  Workflow parityCheck 684e7023878a78b8ac8338ab is completed in time: 6.225s ๐
๐ How It Works
- Each step is an asyncfunction.
- 
goto(step, state)passes updated state and moves to the next step.
- State is persisted between steps.
- Calling goto(null, state)finish the workflow instance.
๐ Real-World Use Cases
- User onboarding
- Approval flows
- Reminders & timeouts
- Background jobs
๐ง Code-first = AI-ready
One underrated benefit of using a code-first workflow engine like Codehooks? It plays beautifully with AI tools like ChatGPT, Cursor and Copilot.
Because workflows are written in plain, readable JavaScript:
- โ You can ask an LLM to generate your workflows from natural language specs
- ๐ You can debug or optimize a step-by-step flow by pasting the code into ChatGPT
- ๐ You can analyze edge cases and verify paths programmatically or through simulations
This makes your workflows not just easier to write โ but easier to evolve, maintain, and reason about, even with AI assistance.
๐ Try it at codehooks.io
 
 
              
 
    
Top comments (0)