Originally published by the Stagepass Editorial Team on stagepass.one.

In short: This guide is the start of a series about getting out of that spot. It covers the three things most Webflow projects end up needing once the custom code grows up: JavaScript, written in TypeScript, for behaviour. CSS, written in SCSS, for everything the Designer does not cover. A small PHP API, for the bit of server-side work Webflow does not run.

You know this form. It started as a plain Webflow contact form: name, email, message, done. Then the client asked for one small extra. Check the email before it goes anywhere. Drop obvious spam. Send the data to the tool the sales team actually uses. Show a proper error when something is missing.

None of that is unusual. But Webflow forms are built to collect submissions and notify you, not to run your own logic on a server.

Sure, you could hand the whole thing to a tool like Make: a webhook catches the form, a scenario does the rest. That works. But now your clients' data takes a detour through someone else's servers before it reaches anyone you trust, the logic lives in someone else's editor, and you have no real say over where that data goes next. On top of that, you've signed up for yet another subscription, one that grows with every form that gets sent.

We explain why each one is worth it, how they fit together in a Stagepass project, what the PHP starter that ships with Stagepass CLI 2.4.0 actually does, and when you should not use any of this.

Three jobs, three pillars

Custom code in Webflow usually does one of three jobs. It is worth keeping them apart, because each one has its own tools and its own failure modes.

Job Pillar Typical examples
Make the page do something JS / TypeScript Filters, sliders, tabs with logic, form helpers, tracking glue
Make the page look like something CSS / SCSS Overrides, states the Designer cannot express, third-party widget styling
Do something on a server PHP Validate and forward form data, call an API with a private key, small webhooks

The first two run in the visitor's browser. The third runs on a server you control, because Webflow hosting does not execute PHP or any other server code.

If you have not set up Stagepass yet, Getting started walks you through it. If you are moving an existing site over, read Move an Existing Project to Stagepass first.

Before: Head Code

So the extras land somewhere else. A script in Head Code. A second script that fixes the first one. A third-party form service with its own account. A few weeks later, nobody wants to touch the page, and every small change means: edit, publish, test, hope.

After: Your own editor

You write the code locally in your own editor. Stagepass builds it. During your own browser session, the loader swaps the production files on the live or staging site for your local ones. Everyone else keeps seeing the published version.

Key takeaway: Stagepass connects all three to your Webflow site the same way. That is the whole idea behind "Stop publishing just to test."

Why bother? What each pillar gives you

TypeScript: fewer surprises on the live site

A Head Code script fails quietly. A typo in a selector, a missing element on one template, a variable that is sometimes null: you find out when a visitor does.

TypeScript catches a good share of these mistakes while you type. Your editor knows that document.querySelector can return nothing and asks you to handle it. It knows what a function expects. When you rename something, it shows you every place that breaks.

You still write plain JavaScript in spirit. The build turns your TypeScript into one bundled file that runs in the browser. Webflow never sees TypeScript at all.

Use cases:

  • Form helpers that read fields, check them and show messages.
  • Interactions that depend on CMS content, like filters or "load more".
  • Glue code between Webflow and a third-party script, such as a booking widget or analytics.
  • Anything you want to split into several files and import, instead of one long block.

SCSS: styles you can find again

Webflow's Designer is great for layout and design. But some styles do not fit in it: states it cannot express, overrides for an embedded widget, small utilities you want everywhere. Those usually end up in a <style> tag, and that tag only grows.

SCSS gives that CSS a structure. You can nest rules, split them into partials, use variables and mixins, and keep comments that never reach the visitor. The build compiles it into one compressed CSS file.

Use cases:

  • Styling a third-party embed that ignores your Webflow classes.
  • Hover, focus and error states for custom form logic.
  • A few shared tokens, kept in one place instead of repeated by hand.

Tip: If you prefer plain CSS, that works too. stagepass new asks.

PHP: a small backend you own

This is the new part. Sometimes the browser is the wrong place for the job:

Watch out: Validation you can trust. Anything checked only in the browser can be skipped. A server check cannot.

  • Private keys. If a form should forward data to a CRM or newsletter tool, the API key must not sit in a public script.
  • Your own rules. Opening hours, a simple allowlist, a lookup, a calculation you do not want to expose.

You could solve each of these with another hosted service. Often that is the right call, and we come back to it below. But if you already have web hosting with PHP, a small API next to your other files is often simpler: one more folder, no extra account, code you can read.

Stagepass CLI 2.4.0 adds an optional starter for exactly that (release notes, changelog). It is deliberately small: plain PHP 8.2 or newer, no framework, no Composer, and no mail sending. The full reference is in the PHP docs.

How it fits together

A typical project with all three pillars looks like this:

  1. stagepass new creates the project and asks which pieces you want.
  2. You write TypeScript in src/, SCSS next to it, and PHP actions in api/.
  3. stagepass dev rebuilds on every save and reloads your live Webflow page in your session. CSS changes swap in without a reload.
  4. Your Webflow page calls the PHP API on your local .sp domain while your session is active.
  5. stagepass build creates the production files in dist/, including a copy of the API without secrets.
  6. You upload dist/ to your host and point Webflow at the hosted files.

Let's walk through it.

Step 1: Start a project

stagepass new 

The wizard asks a handful of questions. Three of them decide the pillars:

Language (ts|js) (ts): Styles (scss|css) (scss): Enable PHP for this project? (y/N): 

It also asks for your staging and optional production domain, and offers to set up Git. With PHP enabled, the project looks roughly like this:

my-project/ src/main.ts # your TypeScript entry src/styles.scss # your styles scripts/build.mjs # the build (esbuild, plus Sass) api/action.php # the one public PHP entry api/actions.php # the list of allowed actions (you edit this) api/actions/ # one small file per action api/lib/ # shared helpers (leave alone) api/.env.example # copy to the project root as .env .php/config.php # optional config, kept out of api/ stagepass.json 

Tip: If PHP is not installed yet, stagepass new still creates the project and tells you what is missing. stagepass doctor checks the local setup any time.

More on requirements further down.

Already have a Stagepass project?

Watch out: stagepass upgrade does not add the PHP starter to an existing project.

The way in is a throwaway project:

stagepass new tmp --php --yes --no-link 

Copy api/ and .php/ from tmp/ into your project, without overwriting files you have already edited. Then set "php": true in stagepass.json, copy api/.env.example to .env in the project root (the folder that contains api/), set ALLOWED_ORIGINS, and run stagepass link again so your .sp domain can run PHP. The PHP docs list the details, including what to do if you have edited scripts/build.mjs.

Step 2: The script (TypeScript)

The generated src/main.ts is a safe starting point for Webflow. It waits until the page is ready, then waits for Webflow when it is on the page, so your code runs after Webflow has set up its own interactions:

(() => { const run = (): void => { // Your project code here. const sp = (window as Window & { sp?: { log?: (...args: unknown[]) => void } }).sp; sp?.log?.('my-project: ready'); }; const start = (): void => { const w = window as Window & { Webflow?: { push?: (fn: () => void) => void }; }; if (w.Webflow && typeof w.Webflow.push === 'function') { w.Webflow.push(run); return; } run(); }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', start, { once: true }); } else { start(); } })(); 

Tip: sp.log comes from the Stagepass loader; without the loader on the page, the line simply does nothing.

Everything lives inside one function, so nothing leaks into the global scope and clashes with other scripts. From here you can import other files, split features into modules and keep each one small. A later part of this series goes deeper.

Step 3: The styles (SCSS)

src/styles.scss starts nearly empty: a CSS variable with your project name and a class named after your project:

// my-project styles :root { --sp-project: 'my-project'; } .sp-my-project { // Add project styles here. } 

Run stagepass dev, open your Webflow page with your session, and change a colour. The new CSS swaps in on save, without a page reload. This is the moment most people stop writing styles in a <style> tag.

Step 4: The PHP API

One door: action.php

The starter has exactly one public PHP file: api/action.php. Every request names an action, and that name has to be on a list. Anything else is rejected. User input is never used as a file name.

The list lives in api/actions.php. A new entry looks like this:

'newsletter' => [ 'file' => 'newsletter.php', // the file in api/actions/ 'methods' => ['POST'], // which HTTP methods are allowed 'honeypot' => true, // check the hidden spam field 'rate_limit' => true, // limit requests per IP 'origin' => 'required', // only allowed websites may call it ], 

You do not need to know much PHP to read that. It is a settings block.

An action is a small function

Each action is one file in api/actions/ that returns a function. The starter ships a contact action. Shortened:

return static function (array $input, array $config): array { $errors = []; validate_required($input, 'name', $errors); validate_max_length($input, 'name', 120, $errors); validate_required($input, 'email', $errors); validate_email($input, 'email', $errors); validate_max_length($input, 'email', 254, $errors); validate_required($input, 'message', $errors); validate_max_length($input, 'message', 4000, $errors); if ($errors !== []) { throw new ValidationException($errors); // becomes a 422 "fail" } // Your logic here (save a record, call an API, enqueue a job). return ['received' => true]; }; 

Note what is missing: the contact action only validates. It does not send email. That is a deliberate choice in the starter. Sending mail touches credentials, deliverability and spam rules, and those decisions belong to you, not to a template.

The starter also does not take over your Webflow form, and there is no official pattern or helper in @stagepass/modules for it yet. You don't need one: a submit listener with preventDefault() and fetch is enough, as Option B below shows.

What comes back

Every answer is JSON with the same three fields: status, data and message, plus a matching HTTP status code. The shape follows the JSend convention:

{ "status": "success", "data": { "received": true }, "message": null } 
{ "status": "fail", "data": { "email": "A valid email is required." }, "message": "Validation failed." } 

fail means the request was rejected: invalid input (422), an unknown action (404), the wrong method (405), a website that is not allowed (403), too many requests (429). error (500) means something broke on the server. The visitor then gets a generic message, and the details only go to the server log.

For your front end, that means one if instead of guesswork.

Calling it from your Webflow page

There are two ways in. Both talk to the same action.php, and both get the same checks.

Option A: a plain form POST. This is the quickest way to see the API work. In Webflow, select the form and open its settings. Under Send to, remove Webflow and Email notifications, add Custom action, enter your API URL, for example https://php.example.com/api/action.php, and choose POST. Give your inputs the names the action expects, here name, email and message. Webflow has no hidden-field element, so add a Code Embed inside the form with two fields: <input type="hidden" name="action" value="contact"> and an empty text field named website that you hide with CSS (the honeypot). The starter reads regular form submissions as well as JSON, so nothing else is needed.

The catch: with a custom action, Webflow's form script steps aside. It only sends forms in the background when they go to Webflow, so your form is sent by the browser itself, like any plain HTML form. The visitor leaves your page and lands on the API's answer, a line of JSON. That happens on success and on failure alike: a missing field shows up as a JSON fail with status 422, not as your error message. Webflow's success and error messages and its redirect setting don't kick in, and the submission won't appear in Webflow's form submissions. That's fine for a first test, but not for a form your visitors use.

There is a second catch, and it matters more for development. The action URL is fixed in Webflow's form settings, so the form always talks to the PHP that is already deployed on your host. To try a change, you deploy the PHP first.

Watch out: To point the form at your local .sp API instead, you change the action in Webflow and publish the site, and then every visitor's form goes there too. That is exactly the publish-to-test loop Stagepass is meant to end.

Option B: JavaScript or TypeScript. Your script sends the form data in the background, reads the answer and shows the result on the page. The visitor stays where they are, and you decide what the thank-you and error states look like. The bigger advantage shows while you develop: the script decides where each request goes. During your own Stagepass session it calls the PHP on your machine, so you test API changes before anything is deployed. Every other visitor keeps calling your live host. Nothing has to be published to test. In TypeScript:

const API_PATH = '/api/action.php'; const LIVE_API = 'https://php.example.com' + API_PATH; // absolute URL of your PHP host // The Stagepass loader. Typed via @stagepass/loader, which the CLI adds to tsconfig. const sp = window.sp; // resolveLocalUrl() returns your local .sp URL only during an active session, // otherwise an empty string, so everyone else gets the live URL. const url = (sp?.vars.isLocal && sp.resolveLocalUrl(API_PATH)) || LIVE_API; const response = await fetch(url, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'contact', name, email, message, website: '', // honeypot: a hidden field that must stay empty }), }); const result = await response.json(); if (result.status === 'success') { // Show your thank-you state. } else { // result.data holds the messages per field. } 

Two things to note. In both options the URL points at your own host, because the Webflow domain cannot answer PHP requests.

Tip: And with stagepass dev running, PHP changes show up on the next request. There is no build step for PHP while you develop.

Option A: form POST Option B: JS/TS
Setup No code, just form settings and two hidden fields A small script in your project
Visitor experience Leaves the page and sees the API's JSON answer, on success and on error Stays on the page and sees your thank-you state
Webflow success and error states Never shown, and the redirect setting is ignored You show them from your script, or build your own
Testing PHP changes Only after you deploy the PHP, or after you change the action in Webflow and publish Locally during your own session, before anything is deployed or published
Field errors The browser checks required and email fields before sending; anything the API rejects only shows up in the JSON Can be shown next to each field
Without JavaScript Still works Needs JavaScript
Best for A first test Any form real visitors use

How to take over a Webflow form cleanly, keep its success and error states, and wire the fields to this call is the topic of a later part.

Key takeaway: Our recommendation: use Option A to see that it works once, and Option B for anything real, and for any PHP you are still working on.

Security, honestly

A public endpoint gets visits you did not ask for. That is normal, not a reason to panic. The starter comes with sensible defaults, and it helps to know what each one does and does not do.

  • Origin allowlist. In .env you list the exact websites that may call the API, for example your .sp domain, your webflow.io staging domain and your live domain. Browsers send that origin with every request, and POST actions refuse anything that is not on the list. This stops other websites from using your API through their visitors' browsers. It does not stop someone who sends requests from a script and fakes the header. That is what the next two defaults are for.
  • Honeypot. A hidden field, website by default, that people never see and simple bots happily fill in. If it is filled, the API answers with a normal-looking success and does nothing, so the bot learns nothing.
  • Rate limit. Requests per IP address and action within a time window. The defaults allow 20 requests per 60 seconds, and you can change both in .env. Treat it as a brake for careless scripts, not as spam protection. In projects created with CLI 2.5.0 or later, two things have changed. If the limiter cannot write its small files on the server, the API answers with HTTP 503 and does not run the action, unless APP_ENV is local, development, or dev; those three keep the old fail-open behaviour. And it uses the address the connection comes from, unless that address is listed in TRUSTED_PROXIES. Only then does it read X-Forwarded-For or Forwarded to find the client. Behind a proxy or CDN, set TRUSTED_PROXIES so visitors are counted separately; otherwise many of them can share one counter. Projects created with CLI 2.4.1 or older stay fail-open and only use the connection address. stagepass upgrade does not change api/; copy the files from a new scaffold, or re-scaffold, as described in the PHP docs.
  • Quiet errors. PHP error display is switched off, and unexpected errors return a generic message. Internals stay in the server log.
  • No secrets in the build. stagepass build copies api/ into dist/, but skips .env, logs, rate-limit data and key, certificate and database files.

Never trust input, even with all of the above on: validate every field in your action.

Secrets never belong in the web root

This is the one rule to take seriously. The API reads its settings, including any API keys you add, from a .env file in the project root, the folder that contains api/. On your host, that folder is usually the web root, the folder the server publishes.

Watch out: Apache and nginx will happily send a file like /.env to anyone who asks for it, as plain text, unless you tell them not to.

Locally you are covered: Caddy, which Stagepass runs for your .sp domain, returns a 404 for every path that starts with a dot, including /.env and /.php/. And stagepass build never copies .env into dist/. Since CLI 2.4.1, the starter also ships a .htaccess that blocks those paths on Apache, and stagepass build copies it into dist/. In a PHP project created with 2.4.0, stagepass upgrade adds the file if it is missing. nginx ignores .htaccess, so there the rule is up to you. For the secrets themselves, you have two options:

  1. Best: use real environment variables. Most hosting panels and servers let you set them per site. The API reads them first, and no .env file has to sit in the web root at all.
  2. Otherwise: a .env file plus a deny rule. Put .env in the folder that contains api/, never in api/ itself, and block hidden paths such as /.env and /.php/ on the server.

The exact rules for Apache and nginx are in the PHP docs, tested and kept up to date there. They block every path that starts with a dot, like Caddy does locally, and leave /.well-known/ alone so certificates keep renewing. On nginx, the rule has to come before your PHP location. Afterwards, open https://php.example.com/.env in the browser. You should get an error page, not your settings.

Tip: Also keep .env out of Git.

Where the PHP runs in production

Webflow does not run PHP. The API needs a separate home: web hosting or a server with PHP 8.2 or newer. If you are unsure what kind of hosting a project needs, Where Do I Put My Files? covers the options.

The flow is short:

  1. Run stagepass build. It copies api/ to dist/api/ and .php/config.php to dist/.php/config.php.
  2. Upload the contents of dist/ so that the folder containing api/ is the document root. The path stays /api/action.php, and the API answers at a URL like https://php.example.com/api/action.php.
  3. Configure it on the host. The build leaves .env out on purpose. Set APP_ENV=production and ALLOWED_ORIGINS to the exact origins of your live and staging sites, ideally as real environment variables. If you use a .env file instead, add the deny rule from the PHP docs first.
  4. Put that absolute URL into your script as the live API address. A relative /api/action.php would go to your Webflow domain, which cannot answer it.

Want a quick check before you upload, without Caddy? Run PHP's built-in server from the folder that contains api/, so the project root, or dist/ after a build:

php -S 127.0.0.1:8080 -t . 

Then http://127.0.0.1:8080/api/action.php?action=ping should answer with JSON.

Watch out: One caution: the built-in server does not hide dot files the way Caddy does, so don't run it in a folder that holds a real .env.

Your built JavaScript and CSS can live on the same host or somewhere else. The loader setup does not change.

What you need locally

  • macOS. Stagepass currently runs on macOS only.
  • PHP 8.2 or newer with php-fpm (or php-cgi). stagepass setup installs PHP through Homebrew. new, doctor, dev and watch check for it and tell you what is missing. The JavaScript and CSS build keeps working either way.
  • Caddy 2.9 or newer is recommended, so the API's cross-origin answers work cleanly on your .sp domain. stagepass doctor shows your version.
  • Node.js 20.17 or newer for the CLI itself. See Installation.

When not to use this

Good tools have edges. Skip some or all of this when:

  • The code is a few lines. Write it locally if you like, then paste it into Webflow. No build, no host.
  • Webflow's own form already does the job. If you only need submissions in your inbox, keep the native form.
  • You have no PHP hosting and do not want to run one. A dedicated form or automation service is a fine choice.
  • You need email, a database, logins or background jobs. The starter is a small front controller, not an application framework. Bigger backends deserve a proper framework.
  • You build reusable React components. Webflow's own tooling may fit better. See Stagepass vs. Webflow DevLink.
  • Your team works on Windows or Linux. Stagepass is macOS-only for now.

Watch out: A server you do not maintain is a liability.

The series

This guide is the overview. The next parts each take one job and go deep, with examples you can copy. Planned so far:

  • TypeScript in Webflow: types, imports and a safe entry point
  • A backend for Webflow forms: taking over a form and talking to the PHP API
  • SCSS for Webflow: structure, partials and live CSS swaps
  • Hosting the PHP API next to Webflow: upload, .env, origins
  • From Head Code to modules: structuring scripts per page and feature
  • Stopping form spam: honeypot, rate limits and origin checks in practice
  • Custom CSS beside Webflow classes: overrides without fighting the Designer
  • The full workflow: from stagepass new to a production upload

Further reading

Frequently asked questions

Can Webflow run PHP?

No. Webflow hosting doesn't execute PHP or any other server code, so the API needs its own home: web hosting or a server with PHP 8.2 or newer. See PHP on Stagepass.

How do I add a backend to a Webflow form?

Send the form to a small PHP API on your own host. A plain form POST is fine for a first test. For a form real visitors use, send the data from a short script, so they stay on the page and see your own thank-you and error states. See PHP on Stagepass.

Can I use TypeScript in Webflow?

Yes, and Webflow never has to know. You write TypeScript locally, and the build turns it into one bundled JavaScript file that runs in the browser. See Getting started.

Does the Stagepass PHP starter send email?

No, and that's on purpose. The contact action only validates. Mail touches credentials, deliverability and spam rules, and those decisions belong to you, not to a template.

How do I keep my .env file safe on Apache or nginx?

Best case, use real environment variables, so no .env file sits in the web root at all. Otherwise keep .env in the folder that contains api/ and block dot paths on the server. Since CLI 2.4.1 the starter ships a .htaccess for Apache, and for nginx the rule is in the PHP docs.