### Install ImageKit.io Next.js SDK Source: https://github.com/imagekit-developer/imagekit-next/blob/master/README.md Install the SDK using npm or yarn. This command adds the necessary package to your project's dependencies. ```bash npm install @imagekit/next ``` -------------------------------- ### Video with ImageKit provider Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Using ImageKit to serve videos. This example assumes the video is hosted on ImageKit and can be accessed via its URL. ```html ``` -------------------------------- ### Video without ImageKit provider Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Standard HTML5 video tag for playing videos. This example does not use ImageKit for video delivery or transformations. ```html ``` -------------------------------- ### Image with error handling Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt An image example demonstrating error handling. The 'data-imagekit-error' attribute is present, indicating a potential loading issue or error state. ```html Image with events ``` -------------------------------- ### Path-Based Transformation Position Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Configure transformations to be appended to the URL path instead of the query string by setting `transformationPosition="path"`. This example shows a resize transformation. ```tsx import { Image } from "@imagekit/next"; // Path-based transformation position Path transformation // Results in: https://ik.imagekit.io/your_imagekit_id/tr:w-400,h-300/demo.jpg ``` -------------------------------- ### Get Upload Auth Params for Next.js App Router Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Generate authentication parameters for client-side uploads using a server-side API route in Next.js App Router. Requires private and public keys. ```tsx // app/api/upload-auth/route.ts (Next.js App Router API Route) import { getUploadAuthParams } from "@imagekit/next/server"; import { NextResponse } from "next/server"; export async function GET() { const authParams = getUploadAuthParams({ privateKey: process.env.IMAGEKIT_PRIVATE_KEY!, publicKey: process.env.IMAGEKIT_PUBLIC_KEY!, // Optional: custom token (defaults to UUID) // token: "custom-unique-token", // Optional: custom expiration in seconds (defaults to 30 minutes) // expire: Math.floor(Date.now() / 1000) + 60 * 60, // 1 hour }); // Returns: { token: string, signature: string, expire: number } return NextResponse.json(authParams); } ``` -------------------------------- ### Get Upload Auth Params for Next.js Pages Router Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Generate authentication parameters for client-side uploads using a server-side API route in Next.js Pages Router. Requires private and public keys. ```tsx // pages/api/upload-auth.ts (Next.js Pages Router API Route) import { getUploadAuthParams } from "@imagekit/next/server"; import type { NextApiRequest, NextApiResponse } from "next"; export default function handler(req: NextApiRequest, res: NextApiResponse) { const authParams = getUploadAuthParams({ privateKey: process.env.IMAGEKIT_PRIVATE_KEY!, publicKey: process.env.IMAGEKIT_PUBLIC_KEY!, }); res.status(200).json(authParams); } ``` -------------------------------- ### Video with all props Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Demonstrates a video tag with multiple attributes including autoplay, loop, muted, playsinline, and a poster image, all served via ImageKit. ```html ``` -------------------------------- ### Fill Container with Custom Query Parameters Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Make an image fill its parent container by setting `fill={true}` and `sizes="100vw"`. You can also add custom query parameters to the URL. ```tsx import { Image } from "@imagekit/next"; // Fill container with custom query parameters
Background
``` -------------------------------- ### Configure ImageKitProvider for Default Settings Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Use ImageKitProvider to set default configuration like urlEndpoint and transformationPosition for all nested Image and Video components. This avoids repetitive configuration. ```tsx import { ImageKitProvider, Image, Video } from "@imagekit/next"; function App() { return ( {/* All nested Image and Video components inherit urlEndpoint */} Hero ); } ``` -------------------------------- ### Basic Image Component Usage Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Render an image with ImageKit by providing the urlEndpoint, src, alt text, width, and height. This is the most basic way to use the Image component. ```tsx import { Image } from "@imagekit/next"; // Basic usage with explicit urlEndpoint Product image ``` -------------------------------- ### Video with transformations Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Apply transformations like height and width to a video served via ImageKit. This allows for resizing and other modifications. ```html ``` -------------------------------- ### Video with urlEndpoint override Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Serve a video from a different ImageKit URL endpoint by overriding the default. This is useful for managing multiple ImageKit accounts or configurations. ```html ``` -------------------------------- ### Responsive Image with srcset Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Use srcset to provide different image sizes for various screen resolutions. Ensure the image URL includes the appropriate transformation parameters. ```html Image with responsive off ``` -------------------------------- ### Video with path transformation Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt Apply transformations directly within the video path. This method allows for more complex path-based manipulations before the video is served. ```html ``` -------------------------------- ### Construct ImageKit URLs with Transformations Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Programmatically constructs ImageKit URLs with transformations. Useful for dynamic URL generation outside of React components. Supports basic URLs, transformations, path-based transformations, query parameters, and chained transformations. ```tsx import { buildSrc } from "@imagekit/next"; // Basic URL construction const imageUrl = buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/default-image.jpg", }); // Output: https://ik.imagekit.io/your_imagekit_id/default-image.jpg // URL with transformations const transformedUrl = buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/product.jpg", transformation: [ { width: 400, height: 300 }, { quality: 80 }, { format: "webp" } ], }); // Output: https://ik.imagekit.io/your_imagekit_id/product.jpg?tr=w-400,h-300:q-80:f-webp // Path-based transformation position const pathBasedUrl = buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/image.jpg", transformation: [{ width: 200, height: 200, crop: "at_max" }], transformationPosition: "path", }); // Output: https://ik.imagekit.io/your_imagekit_id/tr:w-200,h-200,c-at_max/image.jpg // With query parameters const urlWithParams = buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/asset.jpg", transformation: [{ width: 500 }], queryParameters: { version: "v2", updated: "2024" }, }); // Output: https://ik.imagekit.io/your_imagekit_id/asset.jpg?tr=w-500&version=v2&updated=2024 // Chained transformations const chainedUrl = buildSrc({ urlEndpoint: "https://ik.imagekit.io/your_imagekit_id", src: "/photo.jpg", transformation: [ { width: 800, height: 600 }, { effectGray: true }, { blur: 5 } ], }); ``` -------------------------------- ### Image with events and srcset Source: https://github.com/imagekit-developer/imagekit-next/blob/master/test-app/e2e/__snapshot__/pages.spec.ts/Pages-router-test-case-1.txt An image with event handling attributes and responsive srcset. The 'data-imagekit-loaded' attribute indicates successful loading. ```html Image with events ``` -------------------------------- ### Responsive Image with Sizes Attribute Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Configure the Image component to generate responsive srcset for different screen sizes using the sizes attribute. This ensures optimal image loading across devices. ```tsx import { Image } from "@imagekit/next"; // Responsive image with sizes attribute Responsive banner ``` -------------------------------- ### ImageKit Video Component with Ref for Control Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Enable programmatic control of video playback (play/pause) by forwarding a ref to the Video component. ```tsx // Video with ref for programmatic control function VideoPlayer() { const videoRef = useRef(null); const handlePlay = () => videoRef.current?.play(); const handlePause = () => videoRef.current?.pause(); return (
); } ``` -------------------------------- ### Basic ImageKit Video Component Usage Source: https://context7.com/imagekit-developer/imagekit-next/llms.txt Use the Video component for basic video playback with ImageKit URL building. Ensure your urlEndpoint and src are correctly set. ```tsx import { Video } from "@imagekit/next"; import { useRef } from "react"; // Basic video usage