# How do I make an API for my rails app?

**URL:** <https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280>\
**Category:** Rails\
**Created:** [February 3, 2009, 1:07am UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280 "2009-02-03T01:07:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 1:07am UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/1 "2009-02-03T01:07:26Z")

</div>

Hi all,

I’m currently close to releasing my first rails app. Many thanks to the  
countless people that have helped me with my questions on this forum.  
Couldn’t have done it without you.

So being close to release I started thinking of neat things I could do,  
like making an iphone app. I haven’t tried making my own iphone app yet  
and don’t look too forward to it honestly. So I was thinking of making  
an API for my rails app and let others with more experience than me give  
it a shot.

In theory an API shouldn’t be too hard but I wanted to get the opinion  
of others. I’ve tried googling for this but there seems to be no good  
posts that I can find.

Many thanks for any advice/suggestions.

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 5:57am UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/2 "2009-02-03T05:57:09Z")

</div>

What does your Rails application do that other developers will be  
interested in? If you have some neat functionality that you want  
others to use as a library, then release it as a gem. If your entire  
application does something wonderful, put in a SOAP or REST interface  
to it so it can be hosted as a web service.

I fail to see the connection between an iPhone application and the  
Rails application. An iPhone application does give you a wider  
audience thanks to the app store. So it is worth a shot if you a  
have a good idea for an application.

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 6:39am UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/3 "2009-02-03T06:39:16Z")

</div>

Really? Maybe updates? Maybe it’s a game that has a 3d client for  
it? 😉 lots

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 11:42am UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/4 "2009-02-03T11:42:39Z")

</div>

> So being close to release I started thinking of neat things I could do,  
> like making an iphone app. I haven’t tried making my own iphone app yet  
> and don’t look too forward to it honestly. So I was thinking of making  
> an API for my rails app and let others with more experience than me give  
> it a shot.

So you have rails app that does CRUD operations and you would like to do  
those as well from a native mobile application instead than the browser.  
Did I get it?

If so what I did is to implement my app restfully and use XML to execute  
the CRUD operations from the terminal. I didn’t do the terminal  
implementation though…

Is this close to what you were asking?

Regards

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/5 "2009-02-03T15:53:02Z")

</div>

I’ve done some quick searches on REST API as SOAP isn’t recommended by  
the rails community.

Came up with this site:  
[http://hinchcliffe.org/archive/2008/01/10/16613.aspx](http://hinchcliffe.org/archive/2008/01/10/16613.aspx) I’ll take a more  
in depth look later on.

If anyone else has something to contribute, feel free!

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/6 "2009-02-03T14:47:55Z")

</div>

Thanks for the replies Mukund, Julian, and Comopasta!

@Mukund - I consider it to be a wonderful app 😉 I see this app being  
similar and no more complicated than twitterific or tweetie on the  
iphone. I’m thinking a SOAP/REST interface is what I will need. Also,  
the increased visibility of the app store wouldn’t hurt!

@Julian - That game with a 3D client sounds like it would be awesome!  
lol

@Comopasta - Yep, you got the idea! I have CRUD operations but I also  
have some custom methods/actions that I’d like the API to access via a  
native mobile app instead of the browser.

Does anyone know how taxing this is on the server? I think this would be  
a lot easier as there are no stylesheets/javascript/images/etc. to  
request. Unless of course there are millions of requests, in which case  
it wouldn’t be too bad either 😉

Thanks again for the replies!

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/7 "2009-02-03T20:21:49Z")

</div>

Billee D. wrote:

> Hopefully I am not misunderstanding your intent here, so please excuse  
> me if I am missing something.

Hi Billee and thank you for the help!

So far I have all of that pretty much learned. My question (and maybe  
I’m misunderstanding this greatly) is how to make an API for native  
mobile apps to access. XML might be part of the answer but I’m not so  
sure about that.

Most applications with an API require a key (usually a unique and random  
16-26 character alphanumerical code) to confirm you are authorized to  
access the account. I’m sure this key could be substituted by a username  
and password (like twitterific for example). Once this native mobile app  
is authorized to access the account it can do pretty much anything  
someone on the website is allowed to do. Create, Remove, Update, and  
Destroy (CRUD) operations and access some custom actions as well such as  
adding friends.

Thanks.

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 4, 2009, 9:38am UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/8 "2009-02-04T09:38:39Z")

</div>

> Most applications with an API require a key (usually a unique and random  
> 16-26 character alphanumerical code) to confirm you are authorized to  
> access the account. I’m sure this key could be substituted by a username  
> and password (like twitterific for example). Once this native mobile app  
> is authorized to access the account it can do pretty much anything  
> someone on the website is allowed to do. Create, Remove, Update, and  
> Destroy (CRUD) operations and access some custom actions as well such as  
> adding friends.

Hi, yeah keys are one option. I’m using restful\_authentication and I  
modified the plugin by adding some methods to handle the XML calls from  
the mobile. Regarding the keys I actually reused the cookie  
functionality of the same plugin, so for the mobile I set a long  
expiration for the cookie and the client can use the API for as long  
that is valid (logged in). I don’t have the code here but I could post  
my changes here, I would maybe get some feedback about my approach as  
well 🙂

As Billee D. mentioned adding the XML response featyre to the rails app  
is very simple and JSON is also a possibility. I think it also depends  
on what is easier to parse and consume for you on the terminal side.

Cheers.

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [February 3, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/how-do-i-make-an-api-for-my-rails-app/159280/9 "2009-02-03T20:05:57Z")

</div>

Hopefully I am not misunderstanding your intent here, so please excuse  
me if I am missing something.

If you want an API for a Rails app then a RESTful interface is the way  
to go. In your controllers you need to add a respond\_to directive  
inside each method for XML output:

respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @pages }  
end

And then add the appropriate XML view to each action that you want to  
have an RESTful interface to. You may also want to consider adding a  
JSON format to the respond\_to block as well for the sake of  
convenience.

You will also need to set your routes so they are mapped as Rails  
resources:

map.resources :resource

Typically if you use the scaffold or resource generators built into  
Rails these are generated for you and all you have to do is supply the  
appropriate view templates. However you should learn how to build  
things yourself to give you a better understanding of what it takes to  
generate resources in Rails.

I’m sure I might be missing something in my haste, but this is the  
general gist of developing a RESTful interface.

And yes please stay away from SOAP as was mentioned. Rails has a  
better way than SOAP. 🙂

As for the iPhone UI, you may want these:

iPhoney: [http://www.marketcircle.com/iphoney/](http://www.marketcircle.com/iphoney/)

Google iUI JavaScript Library: [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/iui/)

If possible, you may want to pickup a copy of the following book as  
well:

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

This covers some advanced topics, but two of the recipes in the title  
explain how to create a RESTful resource (or resources) and how to  
support an iPhone UI using the Google iUI library linked to above.

Hope this helps at least a little. 🙂
