Moneybird CI

Ruby client for the Moneybird REST API. Supports both personal API tokens and OAuth2 authentication.

[!IMPORTANT] This is a fork of maartenvanvliet/moneybird, this is a very solid implementation that I really wanted to build on top of.

Installation

Add this line to your application's Gemfile:

gem 'moneybird-client'

And then execute:

$ bundle

Usage

client = Moneybird::Client.new('your_bearer_token')

# List administrations
administrations = client.administrations

administration = administrations.first

# List invoices
administration.sales_invoices.all

# List invoices with a specific state
administration.sales_invoices.all(filter: 'state:uncollectible')

# List contacts
administration.contacts.all

# Find contact
administration.contacts.find(moneybird_id)

# Find contact by customer id
administration.contacts.find_by_customer_id(customer_id)

# Create contact
administration.contacts.create(company_name: 'ACME', firstname: 'Foo', lastname: 'Bar')

# Update contact
contact = administration.contacts.all.first
contact.company_name = 'Something new'
administrations.contacts.save(contact)

# Delete contact
administrations.contacts.delete(contact)

# Works similarly with other resources

OAuth2

Moneybird uses OAuth2 for authentication. Use Moneybird::OAuth to handle the flow:

oauth = Moneybird::OAuth.new(
  client_id: "your_client_id",
  client_secret: "your_client_secret",
  redirect_uri: "https://yourapp.com/callback"
)

# Step 1: Redirect user
redirect_to oauth.authorize_url(scope: %w[sales_invoices documents])

# Step 2: Exchange code (in callback)
token = oauth.token(params[:code])

# Step 3: Use the token
client = token.client
administrations = client.administrations

# Step 4: Refresh when needed
new_token = oauth.refresh(token.refresh_token)

Available scopes: sales_invoices, documents, estimates, bank, time_entries, settings.

The authorize_url method accepts an optional state parameter for CSRF protection.

Webhooks

Moneybird, if so configured, sends webhooks to specified endpoints. This gem can deal with these requests.

webhook = Moneybird::Webhook.from_json(request.body)
sales_invoice = webhook.build_entity
sales_invoice.state

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on Codeberg at https://codeberg.org/odeva/moneybird. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.