# Api routes with versions

**URL:** <https://www.ruby-forum.com/t/api-routes-with-versions/177815>\
**Category:** Rails\
**Created:** [October 24, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/api-routes-with-versions/177815 "2009-10-24T14:22:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [October 24, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/api-routes-with-versions/177815/1 "2009-10-24T14:22:55Z")

</div>

Hi,

I’ve been trying to implement a versioned API, I went through  
different versions that I didn’t like.

I am wondering if I can do something like this with routes, and if  
it’s possible how can it be implemented:

[api.example.com/items/show.xml](http://api.example.com/items/show.xml) works, no version  
[api.example.com/v1/items/show.xml](http://api.example.com/v1/items/show.xml) works, v1  
[api.example.com/v2/items/show.xml](http://api.example.com/v2/items/show.xml) works, v2

Basically I am wondering if Rails Routing (or rack) can detect “v1” or  
“v2” and set a params[:version] variable, and then proceed as that  
string is not there (regular routing).

–  
M.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [October 26, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/api-routes-with-versions/177815/2 "2009-10-26T12:14:48Z")

</div>

On Oct 24, 3:20 pm, Marcelo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [api.example.com/v2/items/show.xml](http://api.example.com/v2/items/show.xml) works, v2
> 
> Basically I am wondering if Rails Routing (or rack) can detect “v1” or  
> “v2” and set a params[:version] variable, and then proceed as that  
> string is not there (regular routing).

Why don’t you try

[api.example.com/items/show.xml](http://api.example.com/items/show.xml)  
[apiv1.example.com/items/show.xml](http://apiv1.example.com/items/show.xml)  
[apiv2.example.com/items/show.xml](http://apiv2.example.com/items/show.xml)

This way you won’t need to mess up with the :version param in the  
code–just install separate versions and route them through the web  
server.

Hope this helps.

–  
Alex
