DA5: Code inside the workflow

Lesson 5 of 5 · reading 15–25 minutes; practice and project time is on the module page

Testing and keeping code safe

🎯You'll be able to

Test code steps with pinned awkward data, fail loudly on bad input and keep secrets out of code.

🏠The everyday picture

A tailor measures twice before cutting cloth, because a wrong cut can't be undone. A code step deserves the same care before it changes real records.

📘What it really is

📝A worked example

Before switching on the weekly CRM tidy-up, you build a test checklist:

Test input Expected result Actual
0 contacts Report: before 0, after 0; no errors ✓
3 contacts, no duplicates 3 kept ✓
2 records for one phone, different dates Newer kept, older's filled fields preserved ✓
Contact with no phone Skipped and listed in the report ✗ at first: it vanished silently

The fourth test found a gap: contacts without a phone were dropped from the output. You add them to the report so a person sees them, and rerun every test.

🌍Where it's used

📝Case scenario

A freelancer hands over a workflow with an API key typed into a Code node. The client shares the workflow file with a new agency, and the key is now in someone else's hands. You move the key into n8n credentials, ask the client to replace the key with the provider, and add a handover note that code steps should read keys only from credentials.

🖱️Try it

Write a test table like the one above for your deduplication step from Lesson 4, with at least five rows, including an empty input and a missing field. Run each one with pinned data and record the result.

⚠️Common mistakes

🛠️For your project

Each starter code file has a small self-test. Keep those tests and add your own for the cases your projects meet; they are a quality item in the marking guides.

✅Quick check

Why is throwing an error with a clear message better than quietly skipping bad data?

Show the answer

It stops wrong data from spreading and tells a person exactly what to fix.

Where should an API key used by a workflow be stored?

Show the answer

In the platform's credentials store, not typed into a code step.

← PreviousCommon transformations
ProLearners Assist