Skip to main content
go-swarm-icons
On this page

Iconify Provider

Access over 200,000 icons across 200+ sets via the Iconify API without local setup.

The Iconify provider fetches icons on demand from the HTTP API, giving access to 200,000+ icons across 200+ sets with no files to download.

What it does

IconifyProvider fetches icons from the Iconify HTTP API. It tries multiple hosts in sequence and caches results in memory after the first successful fetch.

When to use it

Reach for this provider during development, prototyping, or in applications where network access is acceptable and the icon set catalog is not known ahead of time.

For offline or build-pipeline use, prefer JSON Collection Provider (the swarm-icons CLI downloads the same sets to disk) or Embedded Lucide. To combine local files with a remote fallback, see Chain Provider.

Setup

Direct construction

Go
import swarmicons "github.com/frostybee/go-swarm-icons"
p := swarmicons.NewIconifyProvider("mdi")
manager := swarmicons.Default("mdi", p)
icon, err := manager.Get("home")

The prefix argument ("mdi") determines which Iconify icon set to query. It maps to the API URL path: https://api.iconify.design/mdi.json?icons=home.

Config builder

Go
import swarmicons "github.com/frostybee/go-swarm-icons"
manager, err := swarmicons.NewConfig().
AddIconifySet("mdi").
DefaultPrefix("mdi").
Build()

Options & behavior

Functional options

Option Default Description
WithTimeout(d time.Duration) 10 seconds HTTP request timeout.
WithHosts(hosts []string) 3 Iconify hosts Override the list of API hosts tried in sequence.
WithHTTPClient(client *http.Client) Default with 10s timeout Provide a custom *http.Client for all requests.
Go
import (
"time"
swarmicons "github.com/frostybee/go-swarm-icons"
)
p := swarmicons.NewIconifyProvider("mdi",
swarmicons.WithTimeout(5 * time.Second),
)

Host fallback

The provider tries hosts in order. If the first host returns a non-200 status or a network error, it moves to the next. The default hosts are:

  1. https://api.iconify.design
  2. https://api.simplesvg.com
  3. https://api.unisvg.com

Override the host list with WithHosts:

Go
import swarmicons "github.com/frostybee/go-swarm-icons"
p := swarmicons.NewIconifyProvider("mdi",
swarmicons.WithHosts([]string{"https://my-iconify-mirror.example.com"}),
)

Request format

Each Get() call (on a cache miss) sends an HTTP GET request:

GET /{prefix}.json?icons={name} HTTP/1.1
User-Agent: SwarmIcons/1.0

The response body is limited to 1 MB. Root-level width and height from the response are used as defaults for icons that omit their own dimensions.

All() returns empty

The Iconify API has no endpoint for listing all icons in a set. All() returns an empty slice. Use it only when a listing is not needed, or combine it with a local provider via Chain Provider.

Has() triggers a network request

If the icon is not already cached, Has() calls Get() internally, which makes an HTTP request.

Caching

Results are cached in a thread-safe sync.RWMutex-protected map. The first successful Get() for a name stores the resolved *Icon. Subsequent calls return the cached value with no network I/O. Failed fetches (network errors, non-200 responses, invalid JSON) are not cached; the provider retries on the next call.

Security

Only the configured hosts are contacted. The default host list restricts requests to the three known Iconify API servers. WithHosts() allows overriding this list for self-hosted mirrors or proxies.

Examples

Minimal

Go
import (
"fmt"
swarmicons "github.com/frostybee/go-swarm-icons"
)
func main() {
p := swarmicons.NewIconifyProvider("mdi")
manager := swarmicons.Default("mdi", p)
icon, err := manager.Get("home")
if err != nil {
fmt.Println("fetch failed:", err)
return
}
fmt.Println(icon.Size(24).ToHTML())
}

Custom timeout and hosts

Go
import (
"fmt"
"time"
swarmicons "github.com/frostybee/go-swarm-icons"
)
func main() {
p := swarmicons.NewIconifyProvider("carbon",
swarmicons.WithTimeout(3 * time.Second),
swarmicons.WithHosts([]string{
"https://api.iconify.design",
"https://api.simplesvg.com",
}),
)
manager := swarmicons.Default("carbon", p)
icon, err := manager.Get("add")
if err != nil {
fmt.Println("not found:", err)
return
}
fmt.Println(icon.ToHTML())
}

JSON Collection vs Iconify API

Dimension JSON Collection Iconify API
When icons are fetched At build/startup (file read or go:embed) On demand per Get() call
Works offline Yes No
Setup effort One swarm-icons json download per set (or go:embed) Zero files needed
Catalog size Only downloaded sets 200,000+ icons across 200+ sets
Caching model Full parse on first call, then in-memory Per-icon HTTP cache in memory
Recommended for Production, SSG pipelines, CI builds Development, prototyping, unknown sets

Choosing a provider

Provider Source Network Disk Caching Best for
Directory .svg files No Yes Per-icon on first access Custom/brand icons
JSON Collection Iconify JSON No Yes (or go:embed) Full parse on first call Production icon sets
Iconify API HTTP API Yes No Per-icon on first fetch Prototyping, unknown sets
Chain Multiple providers Depends Depends Per-provider Local-first + fallback
Embedded Lucide go:embed in binary No No Full parse on first call Quick start, Lucide-only

See also

Edit this page

Last updated: