Skip to content

About

Official Crossly SDKs for Elixir — seller and buyer clients.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Crossly Elixir SDK

ci release Hex license

Official Elixir clients for the Crossly API: list once and sell across every marketplace, read orders, manage inventory, and receive signed webhooks. Generated from the public OpenAPI specs and released automatically when they change, with signed build provenance.

Install

client for install
seller your store: listings, crossposting, orders, analytics {:crossly, "~> 0.2"}
buyer shopping: cart, orders, wishlists, offers, cashback {:crossly_buyer, "~> 0.2"}

Two clients, two credentials

client credential
seller a Personal Access Token (crossly_pat_…, Settings → Developer) or a seller OAuth token
buyer a buyer OAuth token (crossly_oat_…) with buyer:* scopes

They are not interchangeable: a seller token is refused by the buyer API and the reverse, even for the same Crossly account. A 401 from a call that looks right usually means the other client's token.

First call

conn = Crossly.client(System.fetch_env!("CROSSLY_TOKEN"))   # crossly_pat_…

{:ok, page} =
  CrosslySellerAPI.Api.Listings.list_listings(conn, page: 1, limit: 25)
  |> Crossly.unwrap()

for listing <- page.data, do: IO.puts("#{listing.id} #{listing.title} #{listing.status}")

The generated modules take any Tesla client, so they also work without Crossly.client/2:

conn = Tesla.client([{Tesla.Middleware.Headers, [{"authorization", "Bearer " <> System.fetch_env!("CROSSLY_TOKEN")}]}])
{:ok, page} = CrosslySellerAPI.Api.Listings.list_listings(conn, page: 1, limit: 25)

List calls return { data, pagination } and page with page and limit. Money fields named …_cents are integer cents. The generated functions return an API error as {:ok, %CrosslySellerAPI.Model.Error{}} for the statuses the spec lists and {:error, %Tesla.Env{}} for others; Crossly.unwrap/1 (or unwrap!/1, which raises) turns both into {:error, %Crossly.Error{}} with status, code (the API's error.code), message, details and correlation_id, the id support can trace.

Webhooks

Crossly signs every webhook with Crossly-Signature: t=<unix>,v1=<hex HMAC-SHA256 of "t.body">. The verifier ships in the seller and buyer packages and checks the signature in constant time, rejects a timestamp more than five minutes old, and returns the parsed event:

# raw_body: the exact bytes received (in Phoenix, see Crossly.WebhookPlug below)
case CrosslyWebhooks.verify(raw_body, signature_header, System.fetch_env!("CROSSLY_WEBHOOK_SECRET")) do
  {:ok, body} ->
    handle(Jason.decode!(body))   # %{"id", "type", "created", "data"}

  {:error, %CrosslyWebhooks.VerificationError{reason: reason}} ->
    {:rejected, reason}           # :bad_signature, :timestamp_out_of_tolerance, :malformed_header
end

Verify the raw bytes, not a re-serialised body. webhooks/ holds the same verifier on its own, with the shared test vectors every language's port is checked against on each build.

Which package has what

CrosslyWebhooks, Crossly, Crossly.Adapter and Crossly.WebhookPlug ship in crossly only (a module can live in one package of a release). A buyer-only app adds {:crossly, "~> 0.2"} for them; Crossly.client/2 drives CrosslyBuyerAPI.Api.* with a buyer token (crossly_oat_…) exactly as it drives the seller modules.

The client

Crossly.client(token, opts) is a Tesla client for every generated function, with what each integration otherwise writes by hand:

  • Retries: 429, 500, 502, 503, 504, timeouts and dropped connections are retried (3 times by default) with exponential backoff, waiting what Retry-After says when the API sends it.
  • Idempotent writes: every POST and PATCH carries an Idempotency-Key (one per call, the same on each of its retries), so the API replays a retried write instead of applying it twice. Choose the key yourself when the retry may come from another process: Crossly.idempotent("refund-#{order_id}", fn -> … end).
  • Pagination: Crossly.stream(fn page, limit -> … end) is a lazy stream of every row of a { data, pagination } list, stopping at pagination.totalPages (or a short page).
  • Timeout 30 s per attempt; User-Agent crossly-elixir/<version>; errors as Crossly.Error.
Crossly.stream(fn page, limit -> CrosslySellerAPI.Api.Orders.list_orders(conn, page: page, limit: limit) end)
|> Enum.each(&IO.puts(&1.id))

case CrosslySellerAPI.Api.Listings.get_listing(conn, "lst_missing") |> Crossly.unwrap() do
  {:ok, listing} -> listing
  {:error, %Crossly.Error{status: status, code: code, correlation_id: id}} -> IO.puts("#{status} #{code} (#{id})")
end
option default
:base_url https://crossly.net/api e.g. the sandbox, https://sandbox.crossly.net/api
:timeout_ms 30_000 per attempt
:retries 3 0 turns retries off
:backoff_base_ms, :max_backoff_ms, :max_retry_after_ms 500, 20_000, 120_000
:idempotency true stamp POST/PATCH with an Idempotency-Key
:headers [] sent on every request, e.g. [{"x-storefront-id", store_id}]
:user_agent appended after crossly-elixir/<version>
:adapter config :tesla, :adapter, else Tesla.Adapter.Httpc the Tesla adapter that does the HTTP
:on_retry fn attempt, delay_ms, result -> … end, for logging

Phoenix webhooks

Crossly.WebhookPlug verifies the signature over the raw body, halts with 400 {"error": {"code", "message"}} when it does not verify, and otherwise sets conn.assigns.crossly_event (the decoded event) and conn.assigns.crossly_raw_body. Plug.Parsers consumes the body, so keep a copy with the bundled body reader (crossly does not depend on Plug; your Phoenix app already has it):

# endpoint.ex
plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  json_decoder: Phoenix.json_library(),
  body_reader: {Crossly.WebhookPlug.CachingBodyReader, :read_body, []}

# router.ex
pipeline :crossly_webhooks do
  plug Crossly.WebhookPlug, secret: {System, :fetch_env!, ["CROSSLY_WEBHOOK_SECRET"]}
end

scope "/webhooks" do
  pipe_through :crossly_webhooks
  post "/crossly", MyAppWeb.CrosslyWebhookController, :create
end

Runnable versions are in examples/.

Where things are

client API classes models API reference pages
seller seller/lib/crossly_seller_api/api/ seller/lib/crossly_seller_api/model/<area>/ —
buyer buyer/lib/crossly_buyer_api/api/ buyer/lib/crossly_buyer_api/model/<area>/ —

One class per API group (OrdersApi, ListingsApi, …), one model per type, the models grouped one folder per product area (vault, world, order, …). A type several endpoints share is ONE model, named after where it first appears (GetOrderResponse, ListOrdersItem) or, for small shared shapes such as { ok }, after its fields (OkResponse). Each client README lists every method.

Versions and changelog

Semantic versioning. While the version is 0.x, a release that removes an operation or a model bumps the minor version; anything else bumps the patch. Every CHANGELOG entry lists the operations and models that were added, changed or removed in the specs it was generated from.

Verifying a release

Every release artifact is built on GitHub-hosted runners by release.yml and carries a signed SLSA provenance attestation:

gh attestation verify <downloaded-file> --repo crossly-dev/crossly-elixir

Contributing

seller/, buyer/ and webhooks/ are generated, so a pull request that edits them would be overwritten by the next release. Open an issue instead: the fix goes into the API spec or the generator, and every language picks it up. CONTRIBUTING.md explains how to regenerate locally.

More

License

MIT

About

Official Crossly SDKs for Elixir — seller and buyer clients.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages