ExMLS: Reading MLS Listing Feeds From Elixir
I've just published ExMLS 0.1.0 on Hex. It's a small, read-only Elixir library for pulling listings out of an MLS feed over the RESO Web API. The source is on GitHub under the MIT licence, and the API reference is on HexDocs.
Why I needed it
Estate, the real-estate software I rebuilt in Phoenix, is the first consumer. An agency's website is only as useful as the listings on it, and in markets with an MLS those listings come from a feed. Today most MLS providers expose that feed through the RESO Web API, which is OData under the hood: an $metadata document describing the schema, a Property collection, a Media collection, and the usual $select, $filter, $orderby and $top query options.
Talking to it isn't hard, but getting it right takes care: OAuth tokens that expire, pagination links that the server controls, provider-specific fields, and credentials that must never leak to another host. I wanted that work done once, tested, and kept out of Estate's own code, so I made it a library.
What it does
ExMLS deliberately does very little:
- Authentication. Pass a bearer token from your provider, or configure OAuth 2 client credentials with the provider's token endpoint. ExMLS fetches the token on the first request, caches it until it expires and renews it. A 401 from the feed throws the cached token away so the next request gets a fresh one.
- Listings and media.
ExMLS.property/2andExMLS.media/2takeselect:,filter:,orderby:,top:andexpand:and return lazy streams of pages. - Metadata.
ExMLS.metadata/1returns the feed's raw EDMX XML, so you can see which fields your provider actually offers.
That's the whole public API. Persistence, sync checkpoints, removal rules and IDX/VOW display policy belong to the application using it, so they aren't in the library. Write-back and RETS are left out until someone needs them.
Using it
Add it to mix.exs:
defp deps do
[
{:ex_mls, "~> 0.1.0"}
]
end
Build a client. This doesn't touch the network:
client =
ExMLS.new(
service_root: "https://feed.example.com/odata/",
auth:
{:client_credentials,
token_url: "https://auth.example.com/oauth/token",
client_id: System.fetch_env!("MLS_CLIENT_ID"),
client_secret: System.fetch_env!("MLS_CLIENT_SECRET")}
)
Then read listings. Each element of the stream is either {:ok, records} for a page or {:error, %ExMLS.Error{}}, which is always the last thing the stream emits:
client
|> ExMLS.property(
select: "ListingKey,StandardStatus,ListPrice",
filter: "StandardStatus eq 'Active'",
orderby: "ListingKey",
top: 100
)
|> Enum.reduce_while(:ok, fn
{:ok, records}, :ok ->
Enum.each(records, &upsert_listing/1)
{:cont, :ok}
{:error, error}, :ok ->
{:halt, {:error, error}}
end)
The stream follows @odata.nextLink as you enumerate it, so you only fetch as many pages as you consume.
The decisions that matter
Most of the work went into a few rules that are easy to get wrong in an HTTP client.
Keep every field. MLS providers add their own local fields on top of the RESO Data Dictionary. ExMLS returns records as plain maps with string keys, nothing dropped. Mapping them onto a fixed struct would quietly lose the fields an agency often cares about most.
Don't parse the schema. Metadata comes back as raw EDMX instead of a fixed Data Dictionary version baked into the library. The feed's own metadata is the source of truth, and it changes.
Credentials stay on one origin. Feed requests and @odata.nextLink continuations must stay on the service root's scheme, host and port, or they fail with :unsafe_origin. Redirects are returned as errors instead of followed. A server can't send your bearer token somewhere else, even by accident. Continuation query strings are sent exactly as the server gave them.
Partial reads are visible. If page 40 fails after 39 good pages, the stream emits those 39 pages, then the error, then stops. You can't mistake a half-finished sync for a complete one unless you ignore the last element.
Errors are part of the API. Every failure is an %ExMLS.Error{} with a stable reason, such as :unauthorized, :token_request_failed, :http_error (429 throttling included) or :transport_error, and an HTTP status where there is one. Error values and inspect output never contain tokens, secrets, URLs or response bodies, so they're safe to log. Retry and backoff are up to you.
Under the hood it's built on Req, with the OAuth tokens cached in ETS so concurrent requests on one node share a token instead of each asking for its own.
How it was tested
The default test suite runs offline. Beyond that I verified it against two real servers:
- The RESO Reference Server, run locally with SQLite and its synthetic dataset. The integration tests check that unauthenticated reads get a 401, that both bearer and mock OAuth can read metadata and query listings with expanded media, and that all 285 seeded
Mediarecords stream across three server-driven pages. - Bridge Interactive's hosted
testdataset, with Bridge's public test token. Bearer auth, filtered and orderedPropertyqueries, a continuation page, metadata and media embedded inPropertyrecords all worked.
The Bridge run was also a reminder that every provider is a bit different. Its standalone Media collection returned no records, and $expand=Media returned a 400, while $select=ListingKey,Media worked. That's why ExMLS doesn't guess: check your feed's metadata and its documentation, and choose the query it supports. The repository includes an example script that runs the whole read path against either server.
What's next
ExMLS is at 0.1.0 and its job is to feed Estate. As I connect it to more providers I expect to find more of their quirks, and the library will grow from what those feeds need rather than from guesses. If you're building real-estate software in Elixir and have access to an MLS feed, I'd like to hear how it behaves with yours. Issues and pull requests are welcome on GitHub.