A HTTP API client for the terminal
Ain is a terminal HTTP API client. It's an alternative to postman, paw or insomnia.
Ain was built to enable scripting of input and further processing of output via pipes. It targets users who work with many API:s using a simple file format. It uses curl, wget or httpie to make the actual calls.
⭐ Please leave a star if you find it useful! ⭐
You need curl, wget or httpie installed and available on your $PATH. To test this run ain -b. This will generate a basic starter template listing what backends are available on your system in the [Backend] section. It will select one and leave the others commented out.
You can also check manually what backends you have installed by opening a shell and type curl, wget or http (add the suffix .exe to those commands if you're on windows). Any output from the command means it's installed.
On linux or mac one of the three is likely to already be installed. The others are available in your package manager or homebrew.
If you're on windows curl.exe is installed if it's windows 10 build 17063 or higher. Otherwise you can get the binaries via scoop, chocolatey or download them yourself. Ain uses curl.exe and cannot use the curl cmd-let powershell builtin.
You need go 1.13 or higher. Using go install:
go install github.com/jonaslu/ain/cmd/ain@latestUsing the package-manager homebrew
brew install ainUsing the windows package-manager scoop
scoop bucket add jonaslu_tools https://github.com/jonaslu/scoop-tools.git
scoop install ainyay -S ain-binInstall it so it's available on your $PATH:
https://github.com/jonaslu/ain/releases
Ain comes with it's own syntax highlight for vim.
Using vim plug
Plug 'jonaslu/ain', { 'rtp': 'grammars/vim' }Ain comes with a built in basic template that you can use as a starting point. Ain checks what backends (that's curl, wget or httpie) are available on your system and inserts them into the [Backend] section of the generated template. One will be selected and the rest commented out so the template is runnable directly.
Run:
ain -b basic-template.ainThe command above will output a starter-template to the file basic-template.ain.
The basic template calls the / GET http endpoint on localhost with the Content-Type: application/json.
To run the template specify a PORT variable:
ain basic-template.ain --vars PORT=8080See help for all options: ain -h and check out the examples.
.ain.Ain assembles data in template files to build the API-call. Ain parses the data following labels called sections in each template file. Here's a full example:
…The template files can be named anything but some unique ending-convention such as .ain is recommended so you can find them easily.
Ain understands eight [Sections] with each of the sections described in details below. The data in sections either appends or overwrites across template files passed to ain.
Anything after a pound sign (#) is a comment and will be ignored.
ain [OPTIONS] [--vars VAR=VALUE ...]
Ain accepts one or more template-file(s) as a mandatory argument. As sections appends or overwrite you can organize API-calls into hierarchical structures with increasing specificity using files and folders.
You can find examples of this in the examples folder.
Adding an exclamation-mark (!) at the end of a template file name makes ain open the file in your $VISUAL or $EDITOR editor. If none is set it falls back to vim in that order. Once opened you edit the template file for this run only.
Example:
ain templates/get-blog-post.ain! # Lets you edit the get-blog-post.ain for this runAin waits for the editor command to exit. Any terminal editor such as vim, emacs, nano etc will be fine. If your editor forks (as vscode does by default) check if there's a flag stopping it from forking. To stop vscode from forking use the --wait flag:
export EDITOR="code --wait"If ain is connected to a pipe it will read template file names from the pipe. This enables you to use find and a selector such as fzf to keep track of the template-files:
$> find . -name *.ain | fzf -m | ainTemplate file names specified on the command line are read before names from a pipe. This means that echo create-blog-post.ain | ain base.ain is the same as ain base.ain create-blog-post.ain.
When making the call ain mimics how data is returned by the backend. After printing any internal errors of it's own, ain echoes back output from the backend: first the standard error (stderr) and then the standard out (stdout). It then returns the exit code from the backend command as it's own unless there are error specific to ain in which it returns status 1.
Sections are case-insensitive and whitespace ignored but by convention uses CamelCase and are left indented. A section cannot be defined twice in a file. A section ends where the next begins or the file ends.
See escaping If you need a literal section heading on a new line.
Contains the URL to the API. This section appends lines from one template file to the next. This feature allows you to specify a base-url in one file (e g base.ain) as such: http://localhost:3000 and in the next template file specify the endpoint path (e g login.ain): /api/auth/login.
It's recommended that you use the [Query] section below for query-parameters as it handles joining with delimiters and trimming whitespace. You can however put raw query-parameters in the [Host] section too.
Any query-parameters added in the [Query] section are appended last to the URL. The whole URL is properly url-encoded before passed to the backend. The [Host] section must combine to one and only one valid URL. Multiple URLs is not supported.
Ain performs no validation on the url (as backends differ on what a valid url looks like). If your call fails use ain -p as mentioned in troubleshooting to see the resulting command.
The [Host] section is mandatory and appends across template files.
All lines in the [Query] section is appended last to the resulting URL. This means that you can specify query-parameters that apply to many endpoints in one file instead of having to include the same parameter in all endpoints.
An example is if an API_KEY= query-parameter applies to several endpoints. You can define this in a base-file and simply have the specific endpoint URL and possible extra query-parameters in their own file.
Example - base.ain:
[Host]
http://localhost:8080/api
[Query]
API_KEY=a922be9f-1aaf-47ef-b70b-b400a3aa386eget-post.ain
[Host]
/blog/post
[Query]
id=1This will result in the url:
http://localhost:8080/api/blog/post?API_KEY=a922be9f-1aaf-47ef-b70b-b400a3aa386e&id=1The whitespace in a query key / value is only significant within the string.
This means that page=3 and page = 3 will become the same query parameter and page = the next one will become page=the+next+one when processed. If you need actual spaces between the equal-sign and the key / value strings you need to encode it yourself: e g page+=+3 or put
the key-value in the [Host] section where space is significant.
Each line under the [Query] section is appended with a delimiter. Ain defaults to the query-string delimiter &. See the [Config] section for setting a custom delimiter.
All query-parameters are properly url-encoded.
The [Query] section appends across template files.
Headers to pass to the API. One header per line.
Example:
[Headers]
Authorization: Bearer 888e90f2-319f-40a0-b422-d78bb95f229e
Content-Type: application/jsonThe [Headers] section appends across template files.
Http method (e g GET, POST, PATCH). If omitted the backend default is used (GET in both curl, wget and httpie).
Example:
[Method]
POSTThe [Method] section is overridden across template files.
If the API call needs a body (as in the POST or PATCH http methods) the content of this section is passed as a file to the backend with formatting retained.
The file is removed after the API call unless you pass the -l flag. Ain places the file in the $TMPDIR directory (usually /tmp on your box). You can override this in your shell by explicitly setting the $TMPDIR environment variable.
Passing the print command -p flag will cause ain to write out the file named a
No open issues yet, or sync has not completed.