APIRequest (APIR) for Obsidian
repository·master·Indexed 41 days ago
https://github.com/rooyca/obsidian-api-requestAn Obsidian plugin that enables executing API requests directly within notes using 'req' code blocks. It features request caching via UUIDs, JSONPath extraction for precise data selection, support for global and frontmatter variables, and the ability to update note properties from API responses. Version 2.1.3.
What's inside APIRequest
- APIRequest is an Obsidian plugin that allows users to perform API requests directly within their notes. The plugin displays the API responses within code blocks, enabling seamless integration of external data into your Obsidian workflow.
Overview of APIRequest functionality
masterAPIRequest is an Obsidian plugin that allows you to make API requests directly from within your notes. The plugin captures the response from the request and displays it within a code block inside your note.Overview of APIRequest (APIR) features
masterAPIRequest (APIR) is an Obsidian plugin designed to fetch data from APIs and display the responses directly in your notes.
Key capabilities include:
- Flexible HTTP Methods: Supports
GET,POST,PUT, andDELETE. - Variable Support: Use variables from note
front-matter, global variables, or reuse responses from other code blocks. - Data Persistence: Save responses in
localStoragefor easy access and reuse. - Performance Optimization: Ability to disable specific code blocks.
- Data Extraction: Use fine-grained control to extract and display specific values from JSON responses.
- Flexible HTTP Methods: Supports
Security features of APIRequest
masterThe plugin implements several security measures to protect your vault and data:
- Input validation for URLs, paths, and user data.
- XSS prevention through HTML sanitization.
- Protection against directory traversal attacks.
- Secure evaluation of JSONPath expressions.
Pass front-matter properties to API requests
masterYou can dynamically inject values from the current note's YAML front-matter into your API URL using the
{{this.property_name}}syntax. This is useful for context-aware requests, such as searching for a movie based on a title defined in the note's metadata.url: https://api.themoviedb.org/3/search/movie?query={{this.title}}&api_key=YOUR_API_KEY show: $.results[0:].titleManage request identifiers and auto-updates
masterAPIRequest v2 implements unique request identifiers (req-uuid). These identifiers are saved in local storage and are automatically used for subsequent requests to maintain state or context. To override this behavior and generate a new identifier, use theauto-updateflag.Use variables and data re-usage in APIRequest v2
masterAPIRequest v2 provides two primary ways to handle dynamic data and variables:
- Frontmatter Integration: Access variables defined in the file's frontmatter using the
{{this.VARNAME}}syntax. - Global Variables: Manage key/value pairs via the plugin settings.
- Data Re-usage from Local Storage: Access data from previous requests using the
{{ls.UUID>JSONPath}}syntax, wherelsrefers tolocalStorage. This allows you to retrieve a unique request identifier (req-uuid) saved in local storage and use specific parts of its JSON response in subsequent requests.
{{this.MY_VARIABLE}} {{ls.UUID>$.data.id}}- Frontmatter Integration: Access variables defined in the file's frontmatter using the
Reuse API responses with LocalStorage and UUIDs
masterYou can store API responses in
localStorageto reuse them in other codeblocks or notes. To do this, you must assign a unique identifier to the request using thereq-uuidflag.To access a stored response, use the following syntax:
{{ls.UUID>JSONPath}}UUID: The unique identifier defined in thereq-uuidflag (omit thereq-prefix).JSONPath: The JSONPath expression to extract specific data from the response.
Example: If a request uses
req-uuid: user, you can access thenamefield from the response using{{ls.user>$.name}}.```req url: https://api.example.com/profile req-uuid: user<!-- In another note or block --> User Name: {{ls.user>$.name}}
Manage cached responses with `req-uuid`
masterBy providing a
req-uuid, the response is stored inlocalStorage. Subsequent requests with the samereq-uuidwill use the cached data instead of making a new network call.Accessing Cache:
- In another code block: Use the same
req-uuidwithout additional flags. - Via Dataview: Use
localStorage.getItem("req-UUID"). Note that you must prefix the ID withreq-when accessing vialocalStoragedirectly.
Cache Lifecycle:
- Refresh: Add the
auto-updateflag to force a new request. - Remove via Code:
localStorage.removeItem("req-UUID")(in DataviewJS). - Remove via UI: Go to
Settings→APIRequest→Saved API Requestsand delete the entry.
url: https://jsonplaceholder.typicode.com/users/1 req-uuid: my-cached-user// In a DataviewJS block:
dv.paragraph(localStorage.getItem("req-my-cached-user"))- In another code block: Use the same
How to use APIRequest to make API calls in Obsidian
masterTo perform an API request within an Obsidian note, create a code block with the language identifier
req. Inside this block, define your request parameters using YAML-like syntax.Supported parameters include:
url: The endpoint to request.method: The HTTP method (e.g.,GET,POST,PUT,DELETE).body: The request payload.headers: An object containing request headers.show: A JSONPath expression (e.g.,$.id) to extract and display a specific value from the response.req-uuid: A unique identifier for the request, useful for referencing or reusing responses.disabled: A flag to disable the code block to optimize performance.
url: https://my-json-server.typicode.com/typicode/demo/comments method: post body: {"id":1} headers: {"Accept": "application/json"} show: $.id req-uuid: IDpersona disabledAccess API responses in DataviewJS
masterYou can store an API response in
localStorageand then retrieve specific fields using DataviewJS inline queries.- Make the request: Use the
req-uuidproperty in yourreqcode block to assign a unique identifier. Usehiddenif you don't want the raw response rendered in your note. - Retrieve the data: Use
localStorage.getItem("req-<your-uuid>")within a DataviewJS inline query ($= ...). You will typically need toJSON.parse()the retrieved string to access its properties.
Note: The
req-prefix is mandatory when accessing the key inlocalStorage.```req url: https://jsonplaceholder.typicode.com/comments/1 req-uuid: test hidden// Accessing in DataviewJS:
$=dv.el("span", JSON.parse(localStorage.getItem("req-test")).email)- Make the request: Use the
How to use APIRequest (APIR) in Obsidian
masterAPIRequest (APIR) is an Obsidian plugin that allows you to make HTTP requests and display the response directly in your notes using specialized code blocks.
To make a request, create a code block with the language identifier
req. Inside the block, you define parameters such asurl,method,body,headers, andshow(for JSONPath extraction).url: https://my-json-server.typicode.com/typicode/demo/comments method: post body: {"id":1} headers: {"Accept": "application/json"} show: $.id req-uuid: IDpersona disabled