Back

Step by Step Guide to Building a Sage Business Cloud API Integration in Ruby

Aug 11, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Sage Business Cloud API integration? You're in for a treat. This guide will walk you through building a robust integration using Ruby, allowing you to tap into Sage's powerful business management features. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got:

  • Ruby 2.7+ installed
  • Bundler gem
  • A Sage Business Cloud account with API credentials

Got all that? Great! Let's move on.

Setting up the project

First things first, let's set up our Ruby project:

mkdir sage_integration cd sage_integration bundle init

Now, open your Gemfile and add these gems:

gem 'httparty' gem 'oauth2'

Run bundle install, and we're ready to roll!

Authentication

Alright, time to get our hands dirty with OAuth 2.0. Sage uses this for authentication, so let's set it up:

require 'oauth2' client = OAuth2::Client.new( 'YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', site: 'https://oauth.accounting.sage.com', token_url: '/token' ) token = client.client_credentials.get_token

Boom! You've got your access token. Keep it safe; we'll need it for our requests.

Making API requests

Now for the fun part - making API requests. We'll use HTTParty for this:

require 'httparty' class SageAPI include HTTParty base_uri 'https://api.accounting.sage.com/v3.1' def initialize(token) @token = token end def get(endpoint) self.class.get(endpoint, headers: auth_header) end private def auth_header { 'Authorization' => "Bearer #{@token}" } end end sage_api = SageAPI.new(token.token)

Core API operations

Let's break down the main operations:

GET requests

customers = sage_api.get('/customers')

POST requests

new_customer = sage_api.post('/customers', body: { name: 'John Doe' }.to_json)

PUT/PATCH requests

updated_customer = sage_api.put('/customers/123', body: { name: 'Jane Doe' }.to_json)

DELETE requests

sage_api.delete('/customers/123')

Implementing specific use cases

Let's put our new skills to work with a couple of examples:

Fetching customer information

def get_customer(id) response = sage_api.get("/customers/#{id}") JSON.parse(response.body) end customer = get_customer(123) puts "Customer name: #{customer['name']}"

Creating an invoice

def create_invoice(customer_id, items) body = { customer_id: customer_id, invoice_lines: items } response = sage_api.post('/sales_invoices', body: body.to_json) JSON.parse(response.body) end invoice = create_invoice(123, [{ description: 'Web Development', quantity: 1, unit_price: 1000 }]) puts "Invoice created with ID: #{invoice['id']}"

Error handling and best practices

Don't forget to handle those pesky errors and respect rate limits:

def make_request retries = 0 begin yield rescue => e if retries < 3 retries += 1 sleep(2 ** retries) retry else raise e end end end make_request { sage_api.get('/customers') }

Testing the integration

Last but not least, let's write some tests:

require 'rspec' require 'webmock/rspec' RSpec.describe SageAPI do let(:token) { 'fake_token' } let(:sage_api) { SageAPI.new(token) } it 'fetches customers' do stub_request(:get, 'https://api.accounting.sage.com/v3.1/customers') .to_return(body: '{"customers": []}') response = sage_api.get('/customers') expect(JSON.parse(response.body)).to eq({ 'customers' => [] }) end end

Conclusion

And there you have it! You've just built a Sage Business Cloud API integration in Ruby. Pretty cool, right? Remember, this is just the tip of the iceberg. There's so much more you can do with this API, so don't be afraid to explore and experiment.

For more in-depth information, check out the Sage Business Cloud API documentation. Happy coding!