Skip to content
View as Markdown

Svelte avatar library: using DiceBear with Svelte ​

DiceBear works with Svelte 4 and Svelte 5. Use $derived (Svelte 5) or reactive statements (Svelte 4) to keep the avatar in sync with its props, with the JS library for client-side generation or the HTTP API without any dependency.

With the JS library ​

svelte
<script>
  import { Style, Avatar } from '@dicebear/core';
  import lorelei from '@dicebear/styles/lorelei.json' with { type: 'json' };

  const style = new Style(lorelei);

  let { seed = 'Alice' } = $props();

  const avatar = $derived(
    new Avatar(style, {
      seed,
      size: 128,
      // ... other options
    }).toDataUri()
  );
</script>

<img src={avatar} alt="Avatar" />
svelte
<script>
  import { Style, Avatar } from '@dicebear/core';
  import lorelei from '@dicebear/styles/lorelei.json' with { type: 'json' };

  const style = new Style(lorelei);

  export let seed = 'Alice';

  $: avatar = new Avatar(style, {
    seed,
    size: 128,
    // ... other options
  }).toDataUri();
</script>

<img src={avatar} alt="Avatar" />

With the HTTP API ​

svelte
<script>
  let { seed = 'Alice' } = $props();

  const src = $derived.by(() => {
    const url = new URL('https://api.dicebear.com/11.x/lorelei/svg');
    url.searchParams.set('seed', seed);
    url.searchParams.set('size', '128');
    // ... other options
    return url.href;
  });
</script>

<img src={src} alt="Avatar" />
svelte
<script>
  export let seed = 'Alice';

  let src = '';

  $: {
    const url = new URL('https://api.dicebear.com/11.x/lorelei/svg');
    url.searchParams.set('seed', seed);
    url.searchParams.set('size', '128');
    // ... other options
    src = url.href;
  }
</script>

<img src={src} alt="Avatar" />