How to change the configuration of Syncthing service by using the REST API via Python

av | feb 4, 2019

Eser Gözcü works as a DevOps Engineer at Purple Scout. And here he has written an article to save the day for all of you in need of a REST API 101. The ultimate purpose of this article really is to show you how to make basic REST API calls. 

I have seen so many job advertisements that are asking for a REST API knowledge or the relevant experience. Therefore I thought it would be useful to prepare a tutorial at least to being with. Let’s use the REST API of Syncthing.

Syncthing is my favorite file synchronization program, and it does have REST API to pull or push config values.

Following are the steps one can follow/.

1 — Check if the service you intend to interact with has a REST API.

Syncthing indeed has the REST API support for developers. Awesome!

 

2 — Check what kind of endpoints REST API exposes for you and if any of those is an interest to you.

After checking the above link, I see tremendous possibilities to practice/learn REST API.

3 — Let’s start with pulling the configuration values of Syncthing via REST API.

 

Great! I can use /rest/system/config to get the configuration values of my Syncthing Instance 🙂

Below is the initial code with line by line explanation.

 

4 — Execution of this code shows us the configuration details of my Syncthing instance. Sweet 🙂

Great we have the fundamentals of the REST API usage in Python. Basically what we did was to check the documentation of Syncthing to figure out the endpoints and the authentication method. Then it was a matter of selecting an endpoint to play with.

Let’s go one step further, what if not only I want to print the results to the terminal or save the config as .json file but also change a configuration option of our Syncthing instance.

Digging into Syncthing documentation shows me that POST function can be used for /rest/system/config (same endpoint) to push full configuration in the same format we had by using GET before. Doing a little bit more googling reveals that not all configuration options are tied to Syncthing restart when modified.

Here are the details of the configuration options and whether they require service restart or not.

 

Okay, let’s try to change the MAxRecvKbps, this option is to determine limit of the file download speed by Sycnthing. It takes an integer value and does not require a restart after a change.

Syncthing GUI → Actions → Options → Max Recv Kbps can be used if you want to edit this value manually.

Remember, previous code created a config file named config.json

5 — Let’s duplicate that file and rename the new one to config_edited.jsonThere is a line where you can see the defined setting for Max Recv Kbps. Let’s change the value manually to 4000 which is equivalent of 4 Mbps.

 

6 — Above small piece of code will push the config_edited.json as new configuration for our Syncthing instance.

And Syncthing GUI → Actions → Options → Max Recv Kbps also confirms that the result is a success.

I hope this article is helpful. In this article, we learned how to check the documentation of a service with a REST API support. We also used python to pull and push data to our service. If you are also interested in doing same without writing a single line of code, check postman 🙂

Note: Provided Python code is far away from being production-ready quality. No error handling, no function definitions, no object-oriented code whatsoever.

 

Pin It on Pinterest