Skip to content
Loremly
Developers

How to Use a Lorem Ipsum API in JavaScript

Learn how to fetch placeholder text and images in JavaScript with a free Lorem Ipsum API. No key needed, with simple examples you can copy.

Loremly Team

· 4 min read

You can get placeholder text in JavaScript with one fetch call to a Lorem Ipsum API. Loremly's API is free, needs no key and works from the browser because CORS is enabled. This guide shows you the basic calls, the main options and a few small tricks.

What is a Lorem Ipsum API?

An API is a web address that your code can call to get data back. A Lorem Ipsum API sends back filler text instead of a web page. You ask for a few paragraphs, and it returns them as text, HTML, JSON or another format.

This is handy when you build a prototype, a demo or a test page. You don't have to paste long blocks of dummy text into your code. You just ask for what you need.

Two more words you'll see in this guide:

  • CORS is a browser rule about which sites can call an API. Because Loremly allows calls from any site, your front-end code can use it directly.
  • No key means you don't sign up or send a password. You just call the URL.

Your first request

Here is the simplest possible example. It asks for two paragraphs of classic Lorem Ipsum and logs them.

const res = await fetch("https://www.loremly.com/api/v1/lorem?count=2");
const text = await res.text();
console.log(text);

The default format is plain text, so res.text() gives you a normal string. By default you get paragraphs, and they start with the familiar "Lorem ipsum dolor sit amet".

Note that await only works inside an async function or in a JavaScript module. Most modern setups (and the browser console) handle this fine.

Add simple error handling

Networks fail sometimes. A good habit is to check the response before you use it. The res.ok value is true when the request worked.

async function getLorem(params) {
  const url = `https://www.loremly.com/api/v1/lorem?${params}`;
  const res = await fetch(url);
  if (!res.ok) {
    throw new Error(`Request failed with status ${res.status}`);
  }
  return res.text();
}

try {
  const text = await getLorem("count=3");
  document.querySelector("#intro").textContent = text;
} catch (error) {
  console.error(error);
}

Using textContent is safe here. It puts the text on the page as plain text, so nothing inside it can run as code.

Get JSON for lists and cards

If you want separate pieces of text, ask for JSON. This example asks for five sentences.

const res = await fetch(
  "https://www.loremly.com/api/v1/lorem?unit=sentences&count=5&format=json"
);
if (!res.ok) throw new Error(`Request failed: ${res.status}`);

const sentences = await res.json();
// sentences is an array of strings, one per sentence

const list = document.querySelector("#list");
for (const sentence of sentences) {
  const li = document.createElement("li");
  li.textContent = sentence;
  list.appendChild(li);
}

Since res.json() gives you a real array, you can loop over it to fill cards, table rows or list items. This works well for testing how a layout handles several items of different lengths.

Get ready-made HTML

Sometimes you want filler that already has structure, like headings and bullet lists. Use format=html and the elements option.

const res = await fetch(
  "https://www.loremly.com/api/v1/lorem?count=3&format=html&elements=headings,ul"
);
if (!res.ok) throw new Error(`Request failed: ${res.status}`);

const html = await res.text();
document.querySelector("#article").innerHTML = html;

A quick safety note. Only use innerHTML with content you trust, like this generated filler. Never use it with text that users type in, because HTML can contain scripts. For anything you don't control, stick with textContent.

The main parameters

You add options to the URL after a ?, joined with &. Here are the ones you'll use most.

Parameter What it does Values
unit What to count paragraphs, sentences, words, lists, characters
count How many units A number, like 3
flavor The style of text latin, cicero, english, corporate, tech, foodie, space, custom
format The output type text, html, markdown, json, jsx
length Length of each paragraph short, medium, long, mixed
start Begin with "Lorem ipsum dolor sit amet" 1 (yes) or 0 (no)
elements Extra HTML parts, comma separated For example headings,ul
seed Makes the result repeatable Any short word, like demo

A few examples:

  • ?unit=words&count=12 gives you exactly 12 words, which is good for a button or a title.
  • ?flavor=english&length=short gives you short paragraphs in readable English.
  • ?format=markdown&count=4 gives you four paragraphs in Markdown.

The full list of options lives in the API docs.

Get the same text every time

By default, each request returns different text. That's fine for most demos. But for screenshots or automated tests, changing text can be a pain.

Add a seed to fix the result:

const res = await fetch("https://www.loremly.com/api/v1/lorem?count=2&seed=demo");

With the same seed and the same options, you get the same text on every call. Change the seed (say, to demo2) and you get a new, but still fixed, result.

Placeholder images with no code

Images are even easier. The photo URL returns an image, so you can put it straight into an <img> tag. No JavaScript is needed.

<img
  src="https://www.loremly.com/api/v1/photo?w=600&h=400&seed=hero&category=nature"
  alt="Placeholder nature photo"
  width="600"
  height="400"
/>

Here w and h set the size in pixels, seed keeps the same picture each time, and category picks the kind of photo. You can also build these URLs in JavaScript, for example inside a loop that renders product cards. Read the placeholder images guide for more ideas, or try different sizes on the placeholder images page.

A quick tip before you ship

Start with plain text and count, then add options one at a time. That way you always know which change did what. If you want to see the output before you write code, set the same options in the Lorem Ipsum generator first, then copy them into your URL.

And when your project is ready for real users, remember to swap the placeholder calls for real content.

Share

Written by

Loremly Team

We build Loremly, the free Lorem Ipsum and placeholder image generator, and write about design and development workflows.

All posts