> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dreep.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Move URLs to Dreep

> Turn R2 and Cloudflare Images URLs into Dreep URLs with toDreepUrl().

If your images live in R2 and you [connected the bucket](https://docs.dreep.cloud/integrations/cloudflare-r2-transforms.md),
`toDreepUrl()` turns the URLs you use today into Dreep URLs. The file's path and any
transform options stay the same.

```ts theme={null}
import { toDreepUrl } from "dreep";

toDreepUrl("https://images.example.com/cdn-cgi/image/width=400,format=webp/products/1.jpg", {
  from: "https://images.example.com",
  to: "https://cdn.dreep.cloud/your-project",
});
// https://cdn.dreep.cloud/your-project/_img/width=400,format=webp/products/1.jpg
```

It's pure string work: no API key, no client, no network call.

## Signature

```ts theme={null}
toDreepUrl(url: string, options: {
  from: string;
  to: string;
  presets?: Record<string, TransformParams>;
  transform?: TransformParams;
}): string
```

| Parameter | Type | Description |
| :- | :- | :- |
| `url` | `string` | **Required.** The URL you use today |
| `from` | `string` | **Required.** Where the image is served now, such as `https://images.example.com` or `https://pub-….r2.dev` |
| `to` | `string` | **Required.** Where Dreep serves it: your **Images URL** from **Settings** > **Storage**, or your own domain |
| `presets` | `Record<string, TransformParams>` | Settings for presets in the path. See [Presets in the path](#presets-in-the-path) |
| `transform` | [`TransformParams`](https://docs.dreep.cloud/node-sdk/media/build-url#parameters) | Options added to every converted URL |

It swaps `from` for `to`, and turns `/cdn-cgi/image/` into `/_img/`. Cloudflare reserves
`/cdn-cgi/` on every domain it serves, so Dreep reads the same options under `/_img/`.

A URL that doesn't match `from` comes back unchanged, so you can run it over
everything you store.

## Presets in the path

If your own image worker puts a size in the path, like `/i/lightbox/<file>`, mark that
position in `from` with `:preset`:

```ts theme={null}
toDreepUrl("https://r2.example.com/i/lightbox/events/1/photo.jpg", {
  from: "https://r2.example.com/i/:preset",
  to: "https://cdn.dreep.cloud/your-project",
  presets: {
    lightbox: { width: 1600, format: "webp" },
    thumb: { width: 400, height: 400, fit: "cover", format: "webp" },
  },
});
// https://cdn.dreep.cloud/your-project/events/1/photo.jpg?format=webp&width=1600
```

`to` is your **Images URL** from **Settings** > **Storage**. If you've
[pointed your own domain at Dreep](https://docs.dreep.cloud/integrations/cloudflare-r2-transforms.md#optional-your-own-domain),
use that instead, such as `https://images.example.com`.

* A preset listed in `presets` gets those settings.
* Any other preset becomes `?p=<name>`, so create a [Dreep preset](https://docs.dreep.cloud/guides/presets.md) with the
  same name. If there's no such preset, Dreep answers `400`.
* A URL with no file after the preset, like `/i/lightbox`, comes back unchanged.

## Keeping your own domain

If you [point your own domain at Dreep](https://docs.dreep.cloud/integrations/cloudflare-r2-transforms.md#optional-your-own-domain),
pass it as both `from` and `to`. Only `/cdn-cgi/image/` changes:

```ts theme={null}
const domain = "https://images.example.com";

toDreepUrl(`${domain}/cdn-cgi/image/width=400/products/1.jpg`, { from: domain, to: domain });
// https://images.example.com/_img/width=400/products/1.jpg
```

## Updating stored URLs

Run it once over the URLs in your database:

```ts theme={null}
import { toDreepUrl } from "dreep";

const options = {
  from: "https://images.example.com",
  to: "https://cdn.dreep.cloud/your-project",
};

for (const image of await db.images.findMany()) {
  await db.images.update({
    where: { id: image.id },
    data: { url: toDreepUrl(image.url, options) },
  });
}
```

Back up the table first. For a single SQL statement instead, see
[In your database](https://docs.dreep.cloud/integrations/cloudflare-r2-transforms.md#in-your-database).

## Resizing the new URLs

Pass `transform` to add options while converting:

```ts theme={null}
toDreepUrl(image.url, { ...options, transform: { width: 800, format: "webp" } });
```

A preset's settings win where both set the same option. The result also works with
[`url()`](https://docs.dreep.cloud/node-sdk/media/build-url) like any other Dreep URL:

```ts theme={null}
const src = dreep.url(toDreepUrl(image.url, options), { width: 800, format: "webp" });
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.