<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jonas Philbert</title>
    <description>The latest articles on DEV Community by Jonas Philbert (@jonasphilbert).</description>
    <link>https://dev.to/jonasphilbert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F913453%2F07201e1e-f36f-4ff1-83d2-9fb9b4b7d214.jpeg</url>
      <title>DEV Community: Jonas Philbert</title>
      <link>https://dev.to/jonasphilbert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jonasphilbert"/>
    <language>en</language>
    <item>
      <title>Persisting and sharing Insomnia projects using Git</title>
      <dc:creator>Jonas Philbert</dc:creator>
      <pubDate>Tue, 22 Nov 2022 12:29:40 +0000</pubDate>
      <link>https://dev.to/jonasphilbert/persisting-and-sharing-insomnia-projects-using-git-55do</link>
      <guid>https://dev.to/jonasphilbert/persisting-and-sharing-insomnia-projects-using-git-55do</guid>
      <description>&lt;p&gt;&lt;a href="https://insomnia.rest/" rel="noopener noreferrer"&gt;Insomnia&lt;/a&gt; is by far my favorite REST (and GraphQL) client. It's fast and lightweight; perfect for testing and toying with APIs.&lt;/p&gt;

&lt;p&gt;A common use-case when working with a client like Insomnia is to share your collection of requests (your Insomnia project) with others for collaboration, or simply "sharing" with yourself for use with multiple machines/setups.&lt;/p&gt;

&lt;p&gt;Insomnia supports sharing of ones project by using a 90s style "export everything into a single file" -button, accompanied by an "import everything from a single file" one. Excellent for some, far from ideal for others.&lt;/p&gt;

&lt;p&gt;Having knowledge of Git, the 90s approach seems ripe for replacing with a Git-powered solution. So I decided to tackle this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controlling where Insomnia stores its data
&lt;/h2&gt;

&lt;p&gt;Insomnia uses a database structure internally to store the project data (i.e. request collection). Usefully, we can instruct Insomnia where to look for and save said data manually, using the &lt;a href="https://github.com/Kong/insomnia/pull/1230" rel="noopener noreferrer"&gt;&lt;code&gt;INSOMNIA_DATA_PATH&lt;/code&gt;&lt;/a&gt; environment variable.&lt;/p&gt;

&lt;p&gt;Using this, we can launch Insomnia with a custom data path like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ INSOMNIA_DATA_PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;~/.insomnia path/to/insomnia
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;NOTE: In my case (on MacOS), I had to point to Insomnia at: &lt;code&gt;/Applications/Insomnia.app/Contents/MacOS/Insomnia&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If successful, you should see a fresh Insomnia when launching, seen as it no longer points to the default data path.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;NOTE: If you wish to retain your old project, simply use the 90s style import/export from the original data path.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using Git to control the data
&lt;/h2&gt;

&lt;p&gt;Now that Insomnia stores its data in a controlled directory, pushing said data to a Git repository is trivial. We can simply initialize a new Git repository in the data directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/.insomnia
&lt;span class="nv"&gt;$ &lt;/span&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use our favorite Git remote provider (e.g. GitHub, Bitbucket) and connect it to the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatically pulling and pushing changes
&lt;/h2&gt;

&lt;p&gt;While you could manually commit the changes in the data directory every time you finish using Insomnia, I prefer to have it done automatically, as otherwise my human brain will forget. To achieve this, I simply run Insomnia along with a few Git commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/.insomnia &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
git pull &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;span class="nv"&gt;INSOMNIA_DATA_PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;~/.insomnia path/to/insomnia &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
git commit &lt;span class="nt"&gt;-am&lt;/span&gt; :robot: &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I simply wrap the above commands in an alias, and add it to my &lt;code&gt;.*rc&lt;/code&gt; -file, in my case, &lt;code&gt;.zshrc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;insomnia&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;" cd ~/.insomnia &amp;amp;&amp;amp; git pull &amp;amp;&amp;amp; INSOMNIA_DATA_PATH=~/.insomnia path/to/insomnia &amp;amp;&amp;amp; git commit -am :robot: &amp;amp;&amp;amp; git push"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This, of course, completely ignores any merge conflicts that may occur if sharing the repository with someone else who pushes changes to it. As I'm only using this to back up my Insomnia project, I will leave handling merge conflicts as a reader assignment 😗&lt;/p&gt;

&lt;h2&gt;
  
  
  (Optional) Running in the background using tmux
&lt;/h2&gt;

&lt;p&gt;The last icing on top of this solution is running the Insomnia process in the background. When using the alias from before, it will occupy a terminal until Insomnia exits. This is no big deal, but in the interest of keeping things clean, I've opted to run the command in a &lt;a href="https://github.com/tmux/tmux" rel="noopener noreferrer"&gt;tmux&lt;/a&gt; session. Out of sight, out of mind.&lt;/p&gt;

&lt;p&gt;To achieve, this, the alias is simply extended to run inside a detached tmux session, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux new-session &lt;span class="nt"&gt;-s&lt;/span&gt; insomnia &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'
cd ~/.insomnia &amp;amp;&amp;amp;
git pull &amp;amp;&amp;amp;
INSOMNIA_DATA_PATH=~/.insomnia path/to/insomnia &amp;amp;&amp;amp;
git commit -am :robot: &amp;amp;&amp;amp;
git push'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can, once again, simply be wrapped in an alias for convenience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And that's it!&lt;/strong&gt; 🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc63piz94ryovhb0lqw9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc63piz94ryovhb0lqw9w.png" alt="Insomnia data in GitHub" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can now persist your Insomnia project using Git. Thanks for reading.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing Notes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;This solution is likely not robust for sharing with others who active push changes to the collection.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://insomnia.rest/plugins/insomnia-plugin-universal-git" rel="noopener noreferrer"&gt;An Insomnia plugin&lt;/a&gt; exists which will do this for you, but as of writing, it only supports GitLab.&lt;/li&gt;
&lt;li&gt;The popular Insomnia alternative &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; has built-in collaboration features, but is, as of writing, limited to 3 people unless you get your wallet out.h&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
