Skip to content

Rooyca/obsidian-api-request

Repository files navigation

APIR - API Request

GitHub release (latest SemVer) Obsidian plugin release Docs site

req_img

Obsidian plugin that allows you to make requests to API's and receive responses in code-blocks or store them in localStorage.

Important

JSON, TEXT, HTML and MARKDOWN responses supported

Installation

The plugin can be installed from within Obsidian.

Obsidian Community Plugin Browser

  • Go to Settings -> Community plugins
  • Make sure Restricted mode is off
  • Click Browse
  • Search for APIRequest
  • Click Install and then Enable

Usage

There are two ways to use the plugin:

With Code-blocks

To use it, create a code-block with the language set to req. Inside the code-block, you can specify url, method, body, headers, format, etc.

url: https://api.chess.com/pub/player/hikaru/stats
show: chess_daily -> last -> rating

Multiple outputs can be displayed:

url: https://api.chess.com/pub/player/hikaru/stats
show: chess_daily -> last -> rating, chess_daily -> best -> rating
format: <p>Last game: {}</p> <strong>Best game: {}</strong>

It's possible to loop over an array.

url: https://jsonplaceholder.typicode.com/users
show: {..} -> address -> city

Here is a more complex example:

url: https://my-json-server.typicode.com/typicode/demo/comments
format: <h1>{}</h1>
method: post
body: {"id":1}
headers: {"Accept": "application/json"}
show: id

Responses can be stored in localStorage with the req-id flag.

url: https://jsonplaceholder.typicode.com/users/1
show: id
req-id: id-persona

How to get responses from localStorage

For this you'll need dataview.

dv.paragraph(localStorage.getItem("req-id-persona"))

Is mandatory to use req- before whatever you defined in req-id flag.

You could also used inline:

$=localStorage.getItem("req-id-persona")

But this is a little buggy and don't work all the time. (Use this for short and unformatted responses)

How to remove responses from localStorage

In order to remove a response from the localStorage you can use the following code:

localStorage.removeItem("req-id-persona")

For removing all responses use:

localStorage.clear()

Or just go to the plugin settings and click the button Clear ID's to remove all responses.

With Configuration

To use the plugin, press Ctrl+P and search for APIR. There are two options:

  1. Show response in modal
  2. Paste response in current document (at current line)

Settings

The plugin has a few settings that you can configure:

req_settings

  • URL: The URL to send the request to.
  • Format Output: Just JSON blocks (for now).
  • Method: Choose between GET, POST, PUT & DELETE.
  • Body: The data to send with the request. Data should by in JSON format.
  • Headers: The header data to send with the request. Data should by in JSON format. ({"Content-Type": "application/json", "Authorization": "Bearer TOKEN"})
  • Response: The response data to display. If empty all data will be display. You can use a right arrow -> to access nested objects. For example, if you want to show the title from the user object, you can do that like this: user -> title.

To-do

  • Add more request types (POST, PUT, DELETE)
  • Add support for authentication
  • Add customization for modal output

Feedback and Contributions

If you encounter any issues or have feedback on the plugin, feel free to open an issue on the GitHub repository. Contributions are also welcome!