Building a "API Builder". Where to start?

My bosses gave me a challenge, and a good one: to build an 'API Builder.

I imagine I’ll be using a lot of metaprogramming, although I’m not very familiar with it.
I’ll give you a description of the first goal, and maybe you can give me some advice on how to approach the problem or even some study material that could be useful:

”An app where someone can enter the database information, select a table, a set of columns, and transform it into an API endpoint.“

Here, thinking for myself, these problems must have already been solved by someone, but I couldn’t find them.

I would look at ActiveModel and Grape as inspirations. They kind of do this already and you could use use them either directly or as inspiration for what you build.

1 Like

I think you could use rails generators (or a similar tool) to generate a scaffold of the API endpoints (controllers).

For example you could create a new Rails API application: Using Rails for API-only Applications — Ruby on Rails Guides

After that, you can generate a new resource, (for ex: $ bin/rails g scaffold Group name:string) and you’ll get an json endpoint for that model/table: http://localhost:3000/groups.json

If you need you can also create and customize generators or Hanami also has a cli commands that work in a similar fashion. My thinking would be to think of the database schema as input and auto-generated code as output instead of using meta programming.

1 Like

I’ve never heard of Grape! I checked out the website and it looks like an amazing piece of software.

How do you thing I could customize then for skip the migrations generate a file like this:
```ruby
class Person < ApplicationRecord
self.table_name = “people_data”
end
```

I think we need to know more about the requirements.

  • Who’s configuring this?
  • Who’s the end user?
  • Where are you storing the configuration?

That’ll tell you a lot about what parts of ActiveModel you’ll be able to use - you might not be putting the “model” information in a file, it might be loaded from a database table (or tables), and then mapped, and then mapped to API requests and responses (which is why I’d use ActiveModel and Grape as “inspirations” - because you probably can’t use them out of the box).

I see @kplawver
Who’s configuring this? IT personal that are not programmers but need to expose data from their ERP to integrate with third part software.
Who’s the end user? Same person as above.
Where are you storing the configuration? In a local database perhaps?

Here we have several databases in distinguished server. We use Pentaho to “orquestrate” those database, copy fron on to another, backups replications and other things (not my job). However is an intern tool per say.
When we need some integration, is is my job to provide the API. Don’t worry. I’m not programming myselft out of my job. There is a lot more that I do.
So, the Idea is that my friend who works the Pentaho and knows all the database would entre some DB info for conection, select a table, and all or a sort of field and magicaly create a CRUD API.

This is definitely possible, you just have a couple of difficult things to consider:

  • Storing the username and password and making sure that’s secure - be sure to use something like bcrypt to encrypt the password so it’s encrypted at rest.
  • Managing database connections for the configured databases. If this was a limited set of databases you already know about, you might want to make this easier on yourself and just configure those as databases in Rails (in config/database.yml) and then allow your friend to configure the table to use, and fields to expose for the API. If not, then yep, you get to manage the whole thing. Connection management is going to be fun either way, especially with any sort of load on the API.
  • Security for the CRUD APIs - making sure you don’t accidentally give escalated privileges to users accessing the API because of a misconfiguration will be fun.

This is all super possible, but I’d do what you can to make it easy on yourself by trying to keep the database configuration in Rails to start. Good luck and let us know how it goes!

2 Likes