Skip to main content

Level Up Your Website: From Online Media Storage to Smooth Cloudinary Delivery

Level Up Your Website: From Offline Media Chaos to Smooth Cloudinary Delivery

🚀 Level Up Your Website: From Offline Media Chaos to Smooth Cloudinary Delivery

A hands-on tutorial — move media from local folders to a Cloudinary-backed media page with a Next.js API route and a dynamic frontend. Theme: black + light-blue.

On this page

  • Quick pitch
  • API route code
  • Frontend page code
  • Live demo

Why use Cloudinary ?

Cloudinary helps you store, optimize, and deliver images and video from the cloud so your site can scale, load faster, and stop depending on files on your local drive — while giving you automatic resizing, format conversion, and a CDN out of the box.

Cloudinary ?

Cloudinary is a cloud-based media platform designed to manage the entire lifecycle of images and videos — upload, store, transform, optimize and deliver. Instead of checking media files into your repo or serving them from a personal drive, Cloudinary provides a secure hosted space and globally distributed CDN so every visitor receives assets quickly and in the right format for their device.

For developers and teams, Cloudinary offers a developer-first SDK, programmatic APIs, and an easy-to-use dashboard. It automatically supports format negotiation (e.g. WebP/AVIF when supported), on-the-fly transformations (resize, crop, overlays, compression), and video streaming features. This removes heavy client-side resizing and reduces bandwidth usage while providing significantly better perceived performance.

The platform also helps with workflow: tag assets, group by folders, create signed uploads for user-generated content, and integrate with build/deploy pipelines. For projects like a TikTok or YouTube clone, Cloudinary acts as the media backbone — you get consistent URLs, predictable performance, and simple hooks to display anything uploaded to the media library. In short: less manual media management, fewer deployment headaches, and faster pages.

What's the catch?

Cloudinary is powerful but not unlimited. The free tier includes generous capabilities for getting started, but it comes with usage limits: monthly credits (storage + transformations), bandwidth quotas, and API rate limits. If your site becomes popular, you’ll need to watch usage to avoid unexpected overages — and you may want to upgrade to a paid plan.

Transformations (resizing, format conversion) consume transformation usage credits; serving large video files consumes bandwidth. Also, if you use unsigned uploads or expose API keys on the client, you risk abuse — always use signed/upload presets or server-side upload endpoints. Finally, while Cloudinary makes on-the-fly transforms easy, relying on too many heavy server-side transformation chains can cause latency; cache transformed assets when possible (Cloudinary does caching, but plan your workflow).

The fix: treat Cloudinary as part of your architecture — set folder rules, use upload presets and signed uploads, monitor usage in the dashboard, and combine Cloudinary’s CDN with sensible caching and pagination to keep costs and performance predictable.


Today’s demo: build the school's media page

Goal: launch on Netlify or your personal domain and display media dynamically. Upload to Cloudinary via the dashboard (or a server-side upload), then fetch the media list from a Next.js API route and render it on the frontend. When new files are added in Cloudinary they appear on your page without deploying again.

Project folder (Next.js App Router)

// top-level project
Website/
  src/
    app/
      api/
        media/route.js   <-- API route that talks to Cloudinary (we will edit this)
      media/
        page.jsx        <-- media page UI that fetches /api/media (we will edit this)
      globals.css
      layout.jsx
    components/
      MediaGrid.jsx
      MediaCard.jsx
    utilities/
      cloudinaryClient.js
    middleware.js
  package.json
  next.config.js
  README.md

We’ll focus on the two files src/app/api/media/route.js and src/app/media/page.jsx. The rest of the structure shows common supporting files.

Steps — at a glance

  1. Install Cloudinary SDK (server-side): npm install cloudinary
  2. Configure API route to fetch media from Cloudinary (route.js)
  3. Build media page (page.jsx) that fetches /api/media and renders a gallery
  4. Deploy to Netlify/Vercel and point your domain

1. Set up the Cloudinary SDK

Install the official Cloudinary Node SDK in your Next.js project:

npm install cloudinary
// or yarn add cloudinary

2. Create a Cloudinary account and upload files

Quick steps to get credentials:

  • Go to cloudinary.com and sign up for a free account.
  • From the dashboard copy your Cloud Name, API Key, and API Secret. Treat the secret like a password.
  • Upload a few test images or videos from the Media Library → Upload.
  • For user uploads use signed (server-side) upload or upload presets to avoid exposing secrets client-side.

We will store keys in environment variables (e.g., `CLOUDINARY_URL` or `CLOUDINARY_CLOUD_NAME`, `CLOUDINARY_API_KEY`, `CLOUDINARY_API_SECRET`) and never commit them to Git.

3. API route: fetch media from Cloudinary

Save this file as src/app/api/media/route.js. It uses Cloudinary’s Search API to list assets.

// src/app/api/media/route.js
import { v2 as cloudinary } from 'cloudinary';

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME || 'YOUR_CLOUD_NAME',
  api_key: process.env.CLOUDINARY_API_KEY || 'YOUR_API_KEY',
  api_secret: process.env.CLOUDINARY_API_SECRET || 'YOUR_API_SECRET',
});

export async function GET() {
  try {
    const result = await cloudinary.search
      .expression('*')               // fetch everything
      .sort_by('created_at', 'desc')
      .max_results(30)               // change as needed
      .execute();

    // result.resources is an array of asset objects
    return new Response(JSON.stringify(result.resources), {
      status: 200,
      headers: { 'content-type': 'application/json' }
    });
  } catch (err) {
    return new Response(JSON.stringify({ error: err.message }), { status: 500 });
  }
}

4. Frontend: call the API and render media

Save this as src/app/media/page.jsx. This example uses the App Router style and does a client-side fetch for simplicity.

// src/app/media/page.jsx
'use client'
import { useEffect, useState } from 'react';

export default function MediaPage(){
  const [media, setMedia] = useState([]);
  useEffect(() => {
    fetch('/api/media')
      .then(r => r.json())
      .then(data => setMedia(data))
      .catch(err => {
        console.error('Failed to fetch media', err);
      });
  }, []);

  return (
    <main>
      <h1>Media Library</h1>
      <div className="grid">
        {media.map((m) => (
          <article key={m.asset_id}>
            <img src={m.secure_url} alt={m.public_id} loading="lazy" />
          </article>
        ))}
      </div>
    </main>
  );
}

Important tips

  • Use environment variables for credentials. Example: in Vercel/Netlify add `CLOUDINARY_CLOUD_NAME`, `CLOUDINARY_API_KEY`, `CLOUDINARY_API_SECRET`.
  • Prefer signed uploads for user-generated content to avoid exposing secrets.
  • Limit `max_results` and add pagination: store `next_cursor` from Cloudinary’s search result to fetch the next page.
  • Use transformations in URLs (e.g. `/f_auto,q_auto,w_800/`) to automatically serve optimized formats and sizes.

4. Quick example — optimized image URL

https://res.cloudinary.com/YOUR_CLOUD_NAME/image/upload/f_auto,q_auto,w_800/v1621234567/folder/my-photo.jpg

What happens next

Once wired up, uploads to Cloudinary (from the dashboard or your upload endpoints) will show on your media page without redeploys. The CDN serves transformed assets fast and in appropriate formats for each device.

What we learned — checklist

  • Create a Cloudinary account and store credentials as environment variables.
  • Use Cloudinary’s Search API from a server route to list assets.
  • Render media client-side and lazy-load to reduce initial payload.
  • Use on-the-fly transforms for responsive images and lower bandwidth.
  • Monitor usage, use upload presets, and secure uploads with signing.

Live demo (fetches /api/media — falls back to sample data)

If you deploy the Next.js API route, this demo will show your real Cloudinary media. Locally it uses sample assets.

Cloudinary demo • Black + Light Blue theme
Made for launch-ready media pages

Comments

Popular posts from this blog

How to Send Emails from Your React/Next.js App Using EmailJS

How to use your pc as a phone

Running AI models without internet