This repository is intended for *software developers* who wish to modify the [Farmbot Web App](http://my.farmbot.io/). **If you are not a developer**, you are highly encouraged to use [the publicly available web app](http://my.farmbot.io/). Running a server is a non-trivial task which will require an intermediate background in Ruby, SQL and Linux system administration.
This Repo is the RESTful JSON API for Farmbot. This includes things like storage of user data, plant data, authorization tokens and a variety of other resources.
The key responsibility of the API is *information and permissions management*. This should not be confused with device control, which is done via [MQTT](https://github.com/FarmBot/mqtt-gateway).
For a list of example API requests and responses, see our [reference documentation](https://gist.github.com/RickCarlino/10db2df375d717e9efdd3c2d9d8932af). If you wish to write an add-on application that uses the FarmBot API, please let us know in an issue. We are happy to answer any specific questions you may have.
3. [`libpq-dev` and `postgresql`](http://stackoverflow.com/questions/6040583/cant-find-the-libpq-fe-h-header-when-trying-to-install-pg-gem/6040822#6040822)
0. [Install `libpq-dev` and `postgresql`](http://stackoverflow.com/questions/6040583/cant-find-the-libpq-fe-h-header-when-trying-to-install-pg-gem/6040822#6040822)
0.`bundle install`
0.**MOST IMPORTANT STEP**. Copy `config/database.example.yml` to `config/database.yml`. In GNU/Linux or Mac: `mv config/database.example.yml config/database.yml`. **Please read the instructions inside the file. Replace the example values provided with real world values.**
0.`MQTT_HOST=your_mqtt_server_domain rails s -b 0.0.0.0` where `your_mqtt_server_domain` is the IP or domain name of the MQTT server (see next step). The MQTT server does not need to be running yet.
0. Now that the API server is running, [provision an MQTT server](https://github.com/FarmBot/mqtt-gateway).
0. Initialize background workers in a seperate process: `rails jobs:work`. For production setups, this is a required step.
0. (REQUIRES NODE JS > v6) Run `./install_frontend.sh` to build the latest frontend app. You may also run the frontend on a seperate server. The [frontend repository](https://github.com/FarmBot/farmbot-web-frontend) has more info on this if you wish to do so. For most users, `./install_frontend.sh` should be sufficient.
0. Open [localhost:3000](http://localhost:3000). The application is now ready for use.
0. [Raise an issue](https://github.com/FarmBot/Farmbot-Web-API/issues/new?title=Installation%20Failure) if you hit problems with any of these steps.
Doku (a Docker management system) is partially supported. Pull requests welcome. Please see `deployment.md` for more information.
# Config Settings (important)
We try our best to follow the [12 Factor Methodology](https://12factor.net/). Part of that means using ENV variables as a means of [storing configuration](https://12factor.net/config). **Your server won't run without setting ENV variables first**.
You can accomplish this by setting the ENV variables directly from your shell / server management tool or by writing an `application.yml` file.
See `config/application.example.yml` for a list of all the variables that must be set.
**Encryption keys**: Encryption keys will be autogenerated if not present. They can be reset using `rake keys:generate`. If `ENV['RSA_KEY']` is set, it will be used in place of the `*.pem` files. Useful for environments like Heroku, where file system access is not allowed.
**We can't fix issues we don't know about.** Please submit an issue if you are having trouble installing on your local machine.
# Q: How can I Generate an API token?
You must pass a `token` string into most HTTP requests under the `Authorization: ` request header.
Here's what a response looks like when you request a token:
**Important:** The response is provided as JSON for human readability. For your `Authorization` header, you will only be using `data.token.encoded`. In this example, it's the string starting with `eyJ0eXAiOiJ...`
[Low Hanging Fruit](https://github.com/FarmBot/Farmbot-Web-API/search?utf8=%E2%9C%93&q=todo). [Raise an issue](https://github.com/FarmBot/Farmbot-Web-API/issues/new?title=Question%20about%20a%20TODO) if you have any questions.