Let's Learn YAML!
A beginner guide to .yaml and .yml files, covering syntax, common use cases, and a practical tutorial for writing clean configuration files.
What Is YAML?
YAML is a simple format for writing structured data. You will usually see it in configuration files, especially files with .yaml or .yml extensions.
You can think of YAML as a friendly way to describe data without too many brackets, commas, or quotes. It is popular because humans can read it quickly, and programs can parse it into objects, arrays, strings, numbers, and booleans.
For example:
name: Reza
role: Frontend Engineer
active: true
yearsOfExperience: 1That small file describes four values:
nameis a stringroleis also a stringactiveis a booleanyearsOfExperienceis a number
.yml vs .yaml
Both extensions mean the same thing.
You might see:
config.yamlconfig.ymlThey both contain YAML. The .yaml extension is more descriptive, while .yml is shorter and my preference. In daily work, use whichever your project already uses.
Why Developers Use YAML
YAML is often used for configuration because it is easy to scan.
You will find YAML in places like:
- GitHub Actions workflows
- Docker Compose files
- Kubernetes manifests
- CI/CD pipeline settings
- Static site config files
- App environment config
- API documentation tools like OpenAPI
Here is a small GitHub Actions example:
name: CI
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: npm install
- name: Run tests
run: npm testEven if you do not know GitHub Actions yet, you can still guess the shape. The workflow has a name, it runs on push to main, then it runs a test job with several steps.
That readability is the main reason YAML is everywhere.
The Most Important Rule: Indentation Matters
YAML uses indentation to understand structure.
Compare these two examples:
server:
host: localhost
port: 3000server:
host: localhost
port: 3000In the valid version, host and port belong inside server.
Now host and port are not nested under server. Many tools will reject this because the structure is broken.
Use spaces for indentation. Two spaces is the most common style.
Avoid tabs. Tabs can make YAML parsers complain, and the error messages are rarely fun.
Key-Value Pairs
The most basic YAML syntax is a key followed by a value.
appName: Portfolio
environment: production
debug: falseThe key is on the left. The value is on the right.
In JavaScript, this would look similar to:
const config = {
appName: "Portfolio",
environment: "production",
debug: false,
};YAML is basically giving you a cleaner way to write that data in a config file.
Strings
Most simple text values do not need quotes.
title: Basic YAML Guide
author: RezaBut quotes are useful when the value has special characters or might be misunderstood.
version: "1.0"
message: "Build: success"Without quotes, some tools may read 1.0 as a number. With quotes, it stays text.
My simple rule: if the value feels like plain words, skip quotes. If the value looks like a version, URL, date, or command, quotes are safer.
Numbers and Booleans
YAML supports numbers:
port: 3000
timeout: 30
price: 19.99It also supports booleans:
debug: true
cacheEnabled: falseKeep booleans lowercase. That is the style most tools expect.
Lists
Lists use a dash.
tags:
- Frontend
- Testing
- DevOpsIn JavaScript, that would become:
const config = {
tags: ["Frontend", "Testing", "DevOps"],
};Lists are common in YAML because config often has repeated items, like build steps, services, plugins, or allowed domains.
Nested Objects
You can nest data by indenting it.
database:
host: localhost
port: 5432
credentials:
username: admin
password: secretThe structure is:
databasecontainshost,port, andcredentialscredentialscontainsusernameandpassword
This is where indentation becomes very important. Every child item must line up with its siblings.
Lists of Objects
A very common YAML pattern is a list where each item has multiple fields.
projects:
- name: Portfolio
stack: Next.js
deployed: true
- name: Quran Reader
stack: Next.js
deployed: trueEach - starts a new item. The fields under it belong to that item.
This pattern appears a lot in CI/CD:
steps:
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build app
run: npm run buildOnce you understand this pattern, many YAML files become much less scary.
Comments
Comments start with #.
# Runs the app in development mode
environment: development
# Change this to false in production
debug: trueUse comments to explain why something exists, not to repeat what the key already says.
This comment is not helpful:
# Port number
port: 3000This one is better:
# Vercel expects this app to run on port 3000 locally
port: 3000Multi-Line Strings
Sometimes you need a value that spans multiple lines.
Use | when you want to preserve line breaks:
releaseNotes: |
Fixed login bug.
Improved dashboard loading.
Added better error messages.Use > when you want YAML to fold the lines into one paragraph:
description: >
This app helps developers learn YAML
with simple examples and practical usage.Most of the time, you will use | for scripts or long text where line breaks matter.
Environment Variables in YAML
YAML itself does not automatically understand environment variables. The tool reading the YAML decides what syntax is allowed.
For example, Docker Compose supports this:
services:
web:
image: my-app
ports:
- "${APP_PORT}:3000"GitHub Actions uses a different style:
env:
NODE_ENV: production
API_URL: ${{ secrets.API_URL }}Same YAML format, different tool rules.
That is an important mindset: YAML handles the structure, but each tool decides what the values mean.
A Small Tutorial: Create an App Config
Let us write a small YAML config for a web app.
Start with the app identity:
app:
name: Reza Portfolio
version: "1.0.0"
environment: developmentAdd server settings:
server:
host: localhost
port: 3000Add feature flags:
features:
blog: true
quranReader: true
newsletter: falseAdd allowed domains:
allowedDomains:
- localhost
- rezazr.vercel.appNow combine everything:
app:
name: Reza Portfolio
version: "1.0.0"
environment: development
server:
host: localhost
port: 3000
features:
blog: true
quranReader: true
newsletter: false
allowedDomains:
- localhost
- rezazr.vercel.appThat is a real YAML file shape you could use in many projects.
Common Mistakes
Mixing Tabs and Spaces
Use spaces only.
server:
host: localhostserver:
host: localhostForgetting Spaces After Colons
YAML expects a space after the colon in key-value pairs.
name:Rezaname: RezaBreaking List Indentation
steps:
- name: Install
run: npm installsteps:
- name: Install
run: npm installIn the good version, run is aligned under name, so both belong to the same list item.
Leaving Risky Values Unquoted
When in doubt, quote values that look like versions, dates, or special strings.
version: 1.0
date: 2026-05-29
command: npm run buildversion: "1.0"
date: "2026-05-29"
command: "npm run build"This keeps the parser from guessing the wrong type.
A Simple Checklist
Before you commit a YAML file, check these:
- Did you use spaces, not tabs?
- Are child values indented consistently?
- Does every key-value pair have a space after
:? - Are lists aligned correctly?
- Did you quote versions, dates, and commands?
- Did you validate the file with the tool that will read it?
Final Thoughts
YAML is not hard, but it is strict about whitespace. Once you understand indentation, key-value pairs, lists, and nested objects, you can read most .yml files without panic.
The best way to learn it is to open config files you already use, like GitHub Actions or Docker Compose, and read them from top to bottom. Ask yourself: what is a key, what is a value, what is a list, and what is nested inside what?
That habit turns YAML from "weird config syntax" into a language you can scan in a few seconds.
Stay Updated
Get notified when I publish new articles about web development, frontend engineering, and more.
No spam, unsubscribe anytime.