Want to implement APIs in Rails.

Hi, I want to write APIs for my rails 3.1 application but have never
written an API ever. I am supposed to write REST and SOAP APIs. Is
there a formal book or e-book or screencasts or tutorials which
teaches about APIs in Rails and best practices?

I suggest you to read Rails 3 in Action book written by Ryan Bigg and Yehuda Katz.

Get yourself a copy of Service Oriented Design with Ruby and rails

Read through this: http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

and read this: http://blog.steveklabnik.com/2011/07/03/nobody-understands-rest-or-http.html

Make sure that you support oauth2 if you need security. I had some success with this gem here: https://github.com/flowtown/rack-oauth2-server

In general I believe it is better to create a Sinatra rack app as part of your rails app that exposes the API, much cleaner that way. Use Json as the default transport.

As for Soap… avoid it like the plague if you can.

check out ActiveResource too – http://railscasts.com/episodes/94-activeresource-basics