Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
LABS
Glossary

Data URI

A Data URI is a URI scheme that embeds data directly within a document or smart contract, commonly used to store NFT metadata on-chain.
Chainscore © 2026
definition
WEB STANDARD

What is a Data URI?

A Data URI (Uniform Resource Identifier) is a method for embedding small files, such as images or text, directly into HTML, CSS, or JavaScript documents as a single string of characters.

A Data URI (Uniform Resource Identifier) is a URI scheme that allows data to be embedded inline within a web document, eliminating the need for a separate external file request. The scheme is formally defined by RFC 2397 and uses a specific syntax: data:[<mediatype>][;base64],<data>. The optional mediatype specifies the MIME type of the data (e.g., image/png, text/plain), while the base64 indicator denotes that the subsequent data is encoded in Base64, a method for converting binary data into ASCII text. This encoding is essential for non-textual data like images.

The primary technical advantage of a Data URI is the reduction of HTTP requests. By embedding an asset directly into the document's source code, the browser does not need to make a separate network call to fetch it, which can improve page load performance for small, critical resources. Common use cases include embedding small icons, CSS background images, or JavaScript modules. However, this benefit comes with trade-offs: the embedded data is not cached separately by the browser, increases the size of the hosting document, and can make the source code less readable. It is generally recommended for assets under 10KB.

In blockchain and smart contract development, Data URIs are frequently used within token metadata standards like ERC-721 and ERC-1155 for Non-Fungible Tokens (NFTs). Instead of storing image files on-chain—which is prohibitively expensive—the NFT's metadata JSON file often contains a image attribute with a Data URI pointing to the artwork, or the artwork itself can be encoded directly. This allows for self-contained, on-chain representation of digital assets, though gas costs for storing the encoded data can be high. Developers must carefully weigh the permanence of on-chain data against the efficiency of off-chain storage solutions like IPFS.

how-it-works
DATA ENCODING

How Does a Data URI Work?

A Data URI is a mechanism for embedding data directly into a web document, eliminating the need for a separate HTTP request to fetch an external resource.

A Data URI (Uniform Resource Identifier) is a URL scheme that allows developers to embed small files—such as images, fonts, or scripts—directly within HTML, CSS, or JavaScript code. The scheme is defined by the data: prefix, followed by an optional media type (like image/png), an optional base64 indicator, and the encoded data itself. This inline embedding transforms the data into a self-contained string that can be used anywhere a traditional URL is expected, such as in an <img src="..."> tag or a CSS url() function.

The primary technical mechanism involves Base64 encoding, which converts binary data into an ASCII string format safe for transmission over text-based protocols. For example, a small PNG icon can be encoded into a long string of characters prefixed with data:image/png;base64,. While this increases the data size by approximately 33%, it guarantees the resource is available immediately upon document load, a critical feature for generating dynamic content or ensuring offline functionality. For simple text-based data like SVG graphics, the information can be included directly without Base64 encoding using percent-encoding for special characters.

From a performance perspective, Data URIs present a trade-off. The key benefit is the elimination of network latency associated with additional HTTP requests, which can improve load times for critical, small assets. However, because the encoded data is not cached separately by the browser, it must be downloaded every time the containing document is fetched, unlike a traditional external file. Therefore, they are best suited for small, unique, or dynamically generated assets that are unlikely to be reused across multiple pages. Overuse can lead to bloated HTML/CSS files and increased memory usage.

Common use cases include embedding favicons, small CSS background images, and inline SVG graphics to reduce the number of server round trips during the initial page render. They are also fundamental in generating documents programmatically, such as creating PDFs or images on-the-fly in a web application. In blockchain and Web3 contexts, Data URIs are often used within NFT metadata to store token images or attributes directly on-chain or in decentralized storage, though size constraints of smart contracts make this practical only for very small data sets.

When implementing a Data URI, developers must consider the MIME type specification to ensure browsers interpret the data correctly. The syntax follows the pattern: data:[<mediatype>][;base64],<data>. If the base64 token is omitted, the <data> portion is assumed to be URL-encoded text. It's crucial to note that while convenient, Data URIs can negatively impact SEO if overused for images, as search engine crawlers may not process the embedded content as effectively as linked image files with proper alt attributes.

key-features
TECHNICAL PRIMER

Key Features of Data URIs

A Data URI (Uniform Resource Identifier) is a scheme for embedding data directly into a document, eliminating the need for an external HTTP request. This section details its core technical characteristics and use cases.

01

Base64 Encoding

A Data URI uses Base64 encoding to convert binary data (like images) into a safe ASCII text string. This process ensures the data can be transmitted and embedded without corruption. The format is: data:[<mediatype>][;base64],<data>.

  • Example: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...
  • Purpose: Allows embedding of non-textual data within text-based contexts like HTML, CSS, or smart contract metadata.
02

Inline Data Embedding

The primary function of a Data URI is to embed data directly into a host document. This makes the resource self-contained, eliminating external dependencies and network latency.

  • Use Case: In HTML: <img src="data:image/svg+xml,...">
  • Blockchain Application: Storing token metadata or small images directly on-chain within a smart contract, ensuring permanent availability without relying on centralized servers (IPFS).
03

MIME Type Specification

A Data URI must declare a MIME type (e.g., image/png, application/json) to instruct the client how to interpret the embedded data. This is specified immediately after the data: prefix.

  • Syntax: data:<mediatype>;base64,<data>
  • Importance: Without a correct MIME type, browsers and applications cannot properly render or process the embedded content.
04

On-Chain Storage & NFTs

In blockchain, Data URIs are crucial for fully on-chain NFTs and decentralized metadata. The URI is stored in a token's metadata field, pointing to the embedded asset.

  • Mechanism: A smart contract returns a Data URI string for tokenURI().
  • Advantage: The asset is immutable and permanently accessible as part of the blockchain state, unlike HTTP URLs which can break.
05

Size Limitations & Gas Costs

While convenient, Data URIs have significant size limitations due to gas costs on blockchains like Ethereum. Storing large Base64 strings on-chain is prohibitively expensive.

  • Practical Limit: Typically used for small SVGs, JSON metadata, or thumbnails (under ~10KB).
  • Trade-off: The benefit of permanence must be weighed against the high cost of calldata or storage operations.
06

Comparison with Traditional URIs

A Data URI differs from a traditional HTTP/HTTPS URI or an IPFS CID (Content Identifier).

  • HTTP URI: Points to a location; content can change or become unavailable (https://...).
  • IPFS CID: Points to content-addressed data on a distributed network (ipfs://...).
  • Data URI: Is the data itself; completely self-contained and location-independent (data:...).
on-chain-nft-application
DATA URI

Application in On-Chain NFTs

A Data URI is a mechanism for embedding media and metadata directly within a smart contract, enabling fully self-contained on-chain NFTs.

A Data URI (Uniform Resource Identifier) is the primary method for storing an NFT's visual asset and metadata directly on the blockchain. Instead of a traditional web link (https://) pointing to an external server, a Data URI encodes the entire asset—such as an SVG image or JSON metadata—as a base64 string prefixed with a scheme like data:image/svg+xml;base64,. This creates a fully on-chain NFT whose existence is permanently and immutably tied to the contract state, eliminating reliance on any centralized web host. The tokenURI function in an NFT contract returns this encoded string, which clients then decode to render the asset.

The technical implementation involves encoding the asset data. For vector graphics, the entire SVG XML is often written directly into the contract and encoded. For other formats, the binary data is converted to a base64 string. This encoded payload is then concatenated with the appropriate media type (MIME type) to form the complete URI. While powerful, this approach has significant trade-offs: storing data on-chain is extremely expensive in terms of gas fees, and there are practical size limits imposed by block gas limits and contract storage constraints. Consequently, complex or high-resolution media are rarely stored this way.

Prominent examples of projects utilizing Data URIs for on-chain art include Autoglyphs and Chain Runners, which generate SVG artwork algorithmically from the blockchain's state. The key advantage is permanent survivability; the art cannot be altered or lost due to link rot (404 errors) or centralized server failure. This guarantees the NFT's longevity aligns with the blockchain's own. However, developers must carefully optimize code and data to minimize storage costs, often employing techniques like compression and storing only the minimal generative code or seed data required to reconstruct the asset.

examples
DATA URI

Examples & Use Cases

Data URIs are a foundational web standard for embedding small files directly into HTML, CSS, or JavaScript, eliminating the need for separate HTTP requests. This mechanism is widely used for icons, small images, and inline data.

01

Inline Images in HTML/CSS

The most common use case is embedding small images directly into web documents to reduce server requests and improve load times for assets like icons, buttons, and backgrounds.

  • Example: A base64-encoded PNG icon in an <img> tag: <img src="data:image/png;base64,iVBORw0KGgoAAA...">
  • Benefit: Eliminates an HTTP round-trip, crucial for performance-critical assets.
02

Embedding Fonts with @font-face

Web fonts can be embedded directly in CSS using Data URIs, ensuring text renders immediately without waiting for an external font file to download. This is a key technique for FOUT/FOIT (Flash of Unstyled/Invisible Text) prevention.

  • Implementation: The font file is base64-encoded and included in the src property of the @font-face rule.
03

Generating Dynamic Documents

Data URIs enable the client-side generation of files for immediate download or preview without server interaction. This is used in tools that create:

  • PDFs or CSV files from in-browser data.
  • Charts and graphics from user input.
  • The file is created as a Blob or base64 string and set as the href of a download link.
04

Data URI in Smart Contracts

While not for large media, Data URIs are used in blockchain contexts to store metadata or small configuration data on-chain.

  • Example: An NFT's tokenURI might point to a Data URI containing its JSON metadata (name, description) directly encoded, making it fully self-contained on-chain.
  • Trade-off: This is gas-intensive for large data but guarantees permanent, immutable availability.
05

CSS Data URI Sprites

Before widespread HTTP/2, developers used CSS sprites combined with Data URIs to bundle multiple small images (like icons for a UI kit) into a single CSS file.

  • Method: Multiple images are base64-encoded and referenced via background-image properties with precise background-position.
  • Benefit: Reduced the number of HTTP requests to just one for the CSS, a critical performance optimization.
06

Security & Sandboxed Contexts

Data URIs are useful in restricted environments where external resource loading is blocked or undesirable.

  • Use Cases:
    • Browser extensions that need to load icons without network permissions.
    • Sandboxed iframes or secure web applications that must avoid cross-origin requests.
    • Generating self-contained HTML email templates with embedded images.
code-example
IMPLEMENTATION WALKTHROUGH

Code Example: A Simple On-Chain SVG NFT

This section deconstructs a minimal, self-contained NFT smart contract to illustrate the core concepts of on-chain data encoding and rendering.

An on-chain SVG NFT is a non-fungible token where the artwork's source code—written in Scalable Vector Graphics (SVG) markup—is stored entirely within the smart contract's data URI, eliminating reliance on external servers. The following Solidity example demonstrates a contract that programmatically generates and returns a simple SVG image directly from its tokenURI function. This approach ensures the artwork's permanent and immutable availability as long as the blockchain exists.

The contract's core mechanism is the tokenURI function, which must return a Data URI—a single string encoding both the data's MIME type and the content itself. For an SVG, the MIME type is image/svg+xml. The function constructs this URI by concatenating the base prefix data:image/svg+xml;utf8, with the raw SVG markup. This markup is a simple XML document defining a yellow circle on a black background, using tags like <svg>, <circle>, and <text>.

Key technical details include the use of abi.encodePacked for efficient string concatenation in Solidity and the Base64 library for encoding. The SVG properties—such as the circle's cx, cy, and r (center x, center y, and radius)—are hardcoded, but they could be made dynamic based on the token ID or other on-chain state. The resulting Data URI can be pasted directly into a web browser's address bar to view the rendered image, proving its complete on-chain nature.

This pattern's primary advantage is permanence and verifiability; the art is guaranteed by the same consensus securing the token's ownership. However, it introduces trade-offs: storing complex graphics on-chain can become prohibitively expensive due to gas costs, and the SVG standard has limitations compared to raster image formats. This example serves as a foundational template for more sophisticated generative or interactive on-chain art projects.

advantages-vs-disadvantages
DATA URI

Advantages vs. Disadvantages

Data URIs embed data directly within a web document or smart contract, eliminating the need for separate HTTP requests. This approach has distinct trade-offs for performance, security, and decentralization.

01

Performance & Latency

Advantage: Eliminates external HTTP requests, guaranteeing instant availability and reducing page load times. This is critical for on-chain metadata (e.g., NFT traits) that must be available at block finalization.

Disadvantage: Increases the initial document or transaction size. For large data, this can bloat HTML files or make blockchain transactions prohibitively expensive due to gas costs per byte.

02

Reliability & Censorship Resistance

Advantage: Data is self-contained and immutable once stored. For blockchains, this creates permanent, tamper-proof records that do not rely on external servers which can go offline or be censored.

Disadvantage: If the embedded data needs to be updated or corrected, the entire containing document or on-chain asset must be reissued, which is often impossible or costly.

03

Development & Portability

Advantage: Simplifies development by reducing infrastructure dependencies. A single file (e.g., an HTML app or smart contract) contains all necessary data, making it highly portable.

Disadvantage: Lacks dynamic capabilities. The data is static at creation, preventing real-time updates or personalization without redeploying the entire resource. This makes it unsuitable for frequently changing information.

04

Cost & Efficiency

Advantage: Eliminates hosting and bandwidth costs for serving static assets from a web server.

Disadvantage: On blockchains like Ethereum, storing data on-chain is extremely expensive. Embedding data via a Data URI in a transaction or smart contract inflates gas fees significantly compared to storing a hash pointer to off-chain data (e.g., using IPFS).

05

Security Considerations

Advantage: Reduces attack surface by removing reliance on potentially compromised external domains.

Disadvantage: Can be used to embed malicious scripts or large payloads in a single opaque string, complicating security scanning. Base64-encoded Data URIs in NFTs have been used in phishing attacks by disguising malicious SVG content.

06

Use Case: On-Chain NFTs

Perfect for: Fully on-chain generative art (e.g., Art Blocks), where the SVG code and traits are algorithmically generated and stored permanently in the contract. The Data URI ensures the art is immutable and inseparable from the token.

Poor for: NFT projects with high-resolution video or complex 3D models, where file size would make on-chain storage via Data URI economically impossible. These use off-chain storage with an on-chain hash.

ON-CHAIN DATA STORAGE

Data URI vs. External URI (IPFS/HTTPS)

A comparison of methods for referencing data within smart contracts and NFTs, focusing on storage location, persistence, and cost.

FeatureData URI (Inline)IPFS URI (Decentralized)HTTPS URI (Centralized)

Data Location

Stored directly in the transaction/contract

Stored on the IPFS P2P network

Stored on a centralized web server

Persistence

Immutable while chain exists

Immutable (content-addressed)

Mutable (depends on server)

On-Chain Cost

High (gas for calldata/storage)

Low (gas for hash string only)

Low (gas for URL string only)

Data Size Limit

Limited by block gas limits

Effectively unlimited

Effectively unlimited

Censorship Resistance

High (fully on-chain)

High (decentralized network)

Low (single point of failure)

Access Speed

Instant (data on-chain)

Variable (depends on peers)

Fast (if server is responsive)

Long-Term Availability

Guaranteed by blockchain

High (if pinned/replicated)

Low (requires active maintenance)

Common Use Case

Small SVGs, simple metadata

NFT media, decentralized apps

Traditional web2 APIs, prototypes

security-considerations
DATA URI

Security & Gas Considerations

A Data URI (Uniform Resource Identifier) embeds data directly into a smart contract or transaction, bypassing external storage. This has distinct implications for on-chain security and execution costs.

01

On-Chain Immutability

Data embedded via a Data URI is stored permanently on-chain. This creates an immutable, verifiable record, which is ideal for provenance or permanent references. However, it also means sensitive or private data cannot be deleted or altered after deployment, posing a potential privacy risk if misused.

02

Gas Cost Implications

Storing data directly in contract code or calldata is extremely gas-intensive. The cost scales linearly with the size of the data.

  • Example: Storing a 1KB image as a Data URI in a contract's initialization can cost millions of gas, making it prohibitively expensive for large files compared to storing a hash and referencing off-chain data.
03

Calldata vs. Contract Storage

Data URIs can be passed in transaction calldata or stored in contract bytecode.

  • Calldata: Cheaper for one-time use in transactions (non-zero bytes cost gas). Data is not persistent on-chain after the transaction.
  • Contract Storage: Permanently embeds data in the deployed contract's bytecode, leading to high deployment costs and increased cost for all subsequent contract interactions.
05

Smart Contract Size Limit

Ethereum imposes a 24KB size limit on deployed contract bytecode. Embedding large Data URIs can easily cause the contract to exceed this limit, preventing deployment. This forces developers to be judicious with on-chain data or use proxy patterns that separate logic from data.

06

Verification & Auditability

On-chain data is publicly verifiable. Any user or tool can independently confirm that the data referenced by a contract hash matches the originally embedded Data URI. This eliminates reliance on the continued availability of a centralized server, enhancing the trustlessness of the application.

DATA URI

Frequently Asked Questions (FAQ)

A Data URI (Uniform Resource Identifier) is a method for embedding data directly into a document, eliminating the need for external file requests. This glossary section answers common technical questions about its structure, use cases, and implications in web and blockchain development.

A Data URI (Uniform Resource Identifier) is a URI scheme that allows data to be embedded inline within a web page or smart contract, using the format data:[<mediatype>][;base64],<data>. It works by encoding the file's content—such as an image, SVG, or JSON metadata—directly into a text string, which can then be used as a source attribute (e.g., in an <img> tag) or stored in a blockchain transaction. This eliminates the need for a separate HTTP request to fetch an external resource, as all necessary data is contained within the URI itself. The optional ;base64 indicator specifies that the data is Base64-encoded, which is necessary for binary files. In blockchain contexts like NFTs, Data URIs are commonly used for on-chain metadata to ensure permanent, decentralized storage of asset information without relying on external, potentially unreliable servers.

ENQUIRY

Get In Touch
today.

Our experts will offer a free quote and a 30min call to discuss your project.

NDA Protected
24h Response
Directly to Engineering Team
10+
Protocols Shipped
$20M+
TVL Overall
NDA Protected Directly to Engineering Team