APIRequest (APIR) for Obsidian

repository·master·Indexed 41 days ago

https://github.com/rooyca/obsidian-api-request

An 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.

Tokens
8.2K
Snippets
38
Records
50
Agent score
52%

What's inside APIRequest

  1. Overview of APIRequest (APIR) features

    master

    APIRequest (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, and DELETE.
    • Variable Support: Use variables from note front-matter, global variables, or reuse responses from other code blocks.
    • Data Persistence: Save responses in localStorage for 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.
  2. Security features of APIRequest

    master

    The 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.
  3. Pass front-matter properties to API requests

    master

    You 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:].title
  4. Manage request identifiers and auto-updates

    master
    APIRequest 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 the auto-update flag.
  5. Use variables and data re-usage in APIRequest v2

    master

    APIRequest v2 provides two primary ways to handle dynamic data and variables:

    1. Frontmatter Integration: Access variables defined in the file's frontmatter using the {{this.VARNAME}} syntax.
    2. Global Variables: Manage key/value pairs via the plugin settings.
    3. Data Re-usage from Local Storage: Access data from previous requests using the {{ls.UUID>JSONPath}} syntax, where ls refers to localStorage. 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}}
  6. Reuse API responses with LocalStorage and UUIDs

    master

    You can store API responses in localStorage to reuse them in other codeblocks or notes. To do this, you must assign a unique identifier to the request using the req-uuid flag.

    To access a stored response, use the following syntax:

    {{ls.UUID>JSONPath}}

    • UUID: The unique identifier defined in the req-uuid flag (omit the req- prefix).
    • JSONPath: The JSONPath expression to extract specific data from the response.

    Example: If a request uses req-uuid: user, you can access the name field 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}}

  7. Manage cached responses with `req-uuid`

    master

    By providing a req-uuid, the response is stored in localStorage. Subsequent requests with the same req-uuid will use the cached data instead of making a new network call.

    Accessing Cache:

    • In another code block: Use the same req-uuid without additional flags.
    • Via Dataview: Use localStorage.getItem("req-UUID"). Note that you must prefix the ID with req- when accessing via localStorage directly.

    Cache Lifecycle:

    • Refresh: Add the auto-update flag to force a new request.
    • Remove via Code: localStorage.removeItem("req-UUID") (in DataviewJS).
    • Remove via UI: Go to Settings → APIRequest → Saved API Requests and 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"))
  8. How to use APIRequest to make API calls in Obsidian

    master

    To 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
    disabled
  9. Access API responses in DataviewJS

    master

    You can store an API response in localStorage and then retrieve specific fields using DataviewJS inline queries.

    1. Make the request: Use the req-uuid property in your req code block to assign a unique identifier. Use hidden if you don't want the raw response rendered in your note.
    2. Retrieve the data: Use localStorage.getItem("req-<your-uuid>") within a DataviewJS inline query ($= ...). You will typically need to JSON.parse() the retrieved string to access its properties.

    Note: The req- prefix is mandatory when accessing the key in localStorage.

    ```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)

  10. How to use APIRequest (APIR) in Obsidian

    master

    APIRequest (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 as url, method, body, headers, and show (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