Usage | laravel-feed-rust | Spatie       

 SPATIE  

laravel-feed-rust
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-feed-rust](https://spatie.be/docs/laravel-feed-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-feed-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/laravel-feed-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-feed-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-feed-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-feed-rust/v1/usage)
- [ HTTP integration ](https://spatie.be/docs/laravel-feed-rust/v1/http)
- [ Customization ](https://spatie.be/docs/laravel-feed-rust/v1/customization)
- [ Changelog ](https://spatie.be/docs/laravel-feed-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-feed-rust/v1/testing)

 Usage
=====

###  On this page 

1. [ Create entries and choose a format ](#content-create-entries-and-choose-a-format)
2. [ URLs, order and timestamps ](#content-urls-order-and-timestamps)
3. [ Escaping and errors ](#content-escaping-and-errors)
4. [ Discovery links ](#content-discovery-links)

Create entries and choose a format
--------------------------------------------------------------------------------------------------------------------------------------------------------------

- Supply a stable ID, title, timestamp, canonical link, full HTML and author name.
- Chain optional fields, or set the public fields when editing an existing entry.

```
use spatie_feed::{Enclosure, Feed, FeedItem, Format};

let item = FeedItem::new(
    42, "A conversation", "2026-11-30T09:00:00+01:00[Europe/Brussels]".parse()?,
    "/42-a-conversation", "<p>Listen & enjoy.</p>", "Freek",
).author_email("freek@spatie.be")
.categories(["Rust", "Podcasts"])
.enclosure(Enclosure {
    url: "/audio/42.mp3".into(),
    mime_type: "audio/mpeg".into(),
    length: 123456,
});

let mut feed = Feed::new("Conversations", "https://example.com/feed")?
    .format(Format::Rss)
    .items([item]);
let rss = feed.render()?;

feed.format = Format::Json;
let json = feed.render()?;
```

- `Format::Atom` is the default. Atom and RSS responses use `application/xml;charset=UTF-8`; JSON uses `application/json;charset=UTF-8`.
- Discovery links use `application/atom+xml`, `application/rss+xml` and `application/feed+json` respectively.
- Pass raw rendered HTML, not XML-escaped strings. Atom stores it in `summary type="html"`, RSS in `description`, and JSON Feed in `content_html`.
- Enclosures contain a URL, MIME type and byte length. JSON Feed also supports entry images and distinct feed authors by name.
- `items` accepts entries or models implementing `Feedable`. An empty collection needs a type, such as `.items(Vec::<FeedItem>::new())`.

URLs, order and timestamps
------------------------------------------------------------------------------------------------------------------------------------

- `Feed::new` requires an absolute HTTP(S) self URL. It supplies the Atom feed ID and self link.
- Relative entry IDs resolve against the origin: ID `42` in `https://example.com/feed/php` becomes `https://example.com/42`. Absolute IDs such as `urn:post:42` remain unchanged.
- Set `feed.home_page_url = "https://example.com/blog/".parse()?` for an application under a URL prefix. A trailing slash keeps relative paths under `/blog/`; paths starting with `/` resolve at the host root.
- Entries retain their order, count and duplicates. The feed update timestamp is the latest entry by instant, without reordering entries.
- Timestamps use the framework's `ZonedDateTime`. String inputs need a zone annotation such as `[Europe/Brussels]`, `[UTC]` or `[+01:00]`. Output uses whole-second RFC 3339 with the supplied offset, or RFC 2822 for RSS.
- Empty Atom feeds require `feed.updated = Some(updated)`, such as the collection's last publication/removal timestamp. Without it, rendering returns `Error::MissingUpdated` and registered routes return HTTP 500. Nonempty Atom feeds always use their latest entry, even when `feed.updated` is set. Empty RSS and JSON feeds need no timestamp.

Escaping and errors
-----------------------------------------------------------------------------------------------------------------

- XML text and attributes are escaped. Parsing a summary restores the original HTML, including CR/LF, tabs and query strings.
- XML 1.0 forbidden characters produce `Error::InvalidXmlCharacter`; they are never silently deleted. JSON uses JSON escaping, not HTML entities.
- HTML is not sanitized. Supply content safe for your audience.
- `render()` and `to_response()` return typed errors: `Error::Url` for malformed URLs, `Error::InvalidScheme` for non-HTTP(S) feed/home URLs, and `Error::Timestamp` for dates outside RSS's supported range.
- Relative URLs follow standard URL joining, not string concatenation.

Discovery links
-----------------------------------------------------------------------------------------------------

```
use spatie_feed::Feed;

let feed = Feed::new("Posts & links", "https://example.com/feed")?;
let link = feed.discovery_link()?;
```

- Insert the returned markup into the page's head using the native view's `with_html` method.
- Use `discovery_links([&atom, &rss, &json])` for several feeds. Links retain their order without loading entries.
- Set `feed.discovery_type` to override a link's media type, or `feed.content_type` to override the response's complete Content-Type header. Both default to the format's media type. Invalid response headers are omitted by the framework.
- Set `feed.stylesheet = Some("/feed.xsl".into())` for an XML stylesheet instruction in Atom or RSS. It resolves against `home_page_url`. The application serves the stylesheet; JSON ignores it.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
