Modules@robinpath/api
api

@robinpath/api

0.1.2Node.jsPublic

HTTP client for making requests to external APIs with profiles, auth, download/upload, and auto-JSON parsing

API

HTTP client for making requests to external APIs with profiles, auth, download/upload, and auto-JSON parsing

Package: @robinpath/api | Category: Web | Type: Utility

Authentication

api.setAuth "github" "bearer" "ghp_xxxxxxxxxxxx"

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the api module when you need to:

  • Send a GET request to a URL and return the response body (auto-parses JSON) -- Use api.get to perform this operation
  • Send a POST request with a JSON body -- Use api.post to perform this operation
  • Send a PUT request with a JSON body -- Use api.put to perform this operation
  • Send a PATCH request with a partial JSON body -- Use api.patch to perform this operation
  • Send a DELETE request -- Use api.delete to perform this operation

Quick Reference

FunctionDescriptionReturns
getSend a GET request to a URL and return the response body (auto-parses JSON)Parsed JSON body, text string, or full response object if fullResponse is true
postSend a POST request with a JSON bodyParsed JSON body or text string
putSend a PUT request with a JSON bodyParsed JSON body or text string
patchSend a PATCH request with a partial JSON bodyParsed JSON body or text string
deleteSend a DELETE requestParsed JSON body or text string
headSend a HEAD request and return response headers onlyResponse headers as key-value object
downloadDownload a file from a URL and save it to disk{path, size, contentType}
uploadUpload a file as multipart/form-data{status, ok, body}
createProfileCreate a named API profile with base URL, default headers, and timeoutThe created profile configuration
setAuthSet authentication on an existing profile{profileId, auth}
setHeadersMerge additional default headers into an existing profile{profileId, headers}
requestSend a generic HTTP request with an explicit method stringParsed JSON body, text string, or full response object

Functions

get

Send a GET request to a URL and return the response body (auto-parses JSON)

Module: api | Returns: any -- Parsed JSON body, text string, or full response object if fullResponse is true

api.get "https://api.example.com/users"
ParameterTypeRequiredDescription
urlstringYesRequest URL (absolute, or relative if profile has baseUrl)
optionsobjectNo{profile, headers, timeout, fullResponse}

post

Send a POST request with a JSON body

Module: api | Returns: any -- Parsed JSON body or text string

api.post "https://api.example.com/users" {"name": "Alice", "email": "alice@example.com"}
ParameterTypeRequiredDescription
urlstringYesRequest URL
bodyanyYesRequest body (objects auto-serialized to JSON)
optionsobjectNo{profile, headers, timeout, fullResponse}

put

Send a PUT request with a JSON body

Module: api | Returns: any -- Parsed JSON body or text string

api.put "https://api.example.com/users/1" {"name": "Bob"}
ParameterTypeRequiredDescription
urlstringYesRequest URL
bodyanyYesRequest body (objects auto-serialized to JSON)
optionsobjectNo{profile, headers, timeout, fullResponse}

patch

Send a PATCH request with a partial JSON body

Module: api | Returns: any -- Parsed JSON body or text string

api.patch "https://api.example.com/users/1" {"email": "new@example.com"}
ParameterTypeRequiredDescription
urlstringYesRequest URL
bodyanyYesPartial update body
optionsobjectNo{profile, headers, timeout, fullResponse}

delete

Send a DELETE request

Module: api | Returns: any -- Parsed JSON body or text string

api.delete "https://api.example.com/users/1"
ParameterTypeRequiredDescription
urlstringYesRequest URL
optionsobjectNo{profile, headers, timeout, fullResponse}

head

Send a HEAD request and return response headers only

Module: api | Returns: object -- Response headers as key-value object

api.head "https://api.example.com/files/doc.pdf"
ParameterTypeRequiredDescription
urlstringYesRequest URL
optionsobjectNo{profile, headers, timeout}

download

Download a file from a URL and save it to disk

Module: api | Returns: object -- {path, size, contentType}

api.download "https://example.com/report.pdf" "./report.pdf"
ParameterTypeRequiredDescription
urlstringYesURL of the file to download
filePathstringYesLocal path to save the file
optionsobjectNo{profile, headers, timeout}

upload

Upload a file as multipart/form-data

Module: api | Returns: object -- {status, ok, body}

api.upload "https://api.example.com/upload" "./photo.jpg" "image"
ParameterTypeRequiredDescription
urlstringYesUpload endpoint URL
filePathstringYesLocal path of the file to upload
fieldNamestringNoForm field name (default: 'file')
optionsobjectNo{profile, headers, timeout, fields}

createProfile

Create a named API profile with base URL, default headers, and timeout

Module: api | Returns: object -- The created profile configuration

api.createProfile "github" {"baseUrl": "https://api.github.com", "headers": {"Accept": "application/vnd.github.v3+json"}}
ParameterTypeRequiredDescription
idstringYesUnique profile identifier
optionsobjectYes{baseUrl, headers, timeout}

setAuth

Set authentication on an existing profile

Module: api | Returns: object -- {profileId, auth}

api.setAuth "github" "bearer" "ghp_xxxxxxxxxxxx"
ParameterTypeRequiredDescription
profileIdstringYesProfile ID to configure
typestringYesAuth type: 'bearer', 'basic', or 'apikey'
tokenstringYesAuth token or credentials (for basic: 'user:pass')
optionsobjectNo{headerName} — custom header name for apikey auth

setHeaders

Merge additional default headers into an existing profile

Module: api | Returns: object -- {profileId, headers}

api.setHeaders "github" {"X-Custom": "value"}
ParameterTypeRequiredDescription
profileIdstringYesProfile ID to update
headersobjectYesHeaders to merge into profile defaults

request

Send a generic HTTP request with an explicit method string

Module: api | Returns: any -- Parsed JSON body, text string, or full response object

api.request "OPTIONS" "https://api.example.com/resource"
ParameterTypeRequiredDescription
methodstringYesHTTP method (GET, POST, PUT, PATCH, DELETE, etc.)
urlstringYesRequest URL
optionsobjectNo{body, profile, headers, timeout, fullResponse}

Error Handling

All functions throw on failure. Common errors:

ErrorCause
URL is requiredCheck the error message for details
File path is requiredCheck the error message for details
Download failed: ${response.status} ${response.statusText}Check the error message for details
Profile ID is requiredCheck the error message for details
Auth type is requiredCheck the error message for details
Token is requiredCheck the error message for details
Invalid auth type "...". Must be "bearer", "basic", or "apikey".Check the error message for details
Profile "..." not found. Create it first with api.createProfile.Check the error message for details
@desc "Get and validate result"
do
  set $result as api.get "https://api.example.com/users"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Setup authentication"
do
  api.setAuth $token
enddo

@desc "Get and iterate results"
do
  set $result as api.get "https://api.example.com/users"
  each $item in $result
    print $item
  end
enddo

2. Create a new item with post

Create a new resource and capture the result.

@desc "Setup authentication"
do
  api.setAuth $token
enddo

@desc "Post"
do
  set $result as api.post "https://api.example.com/users" {"name": "Alice", "email": "alice@example.com"}
  print "Created: " + $result
enddo

3. Check before creating

List existing items and only create if needed.

@desc "Setup authentication"
do
  api.setAuth $token
enddo

@desc "Get and post"
do
  set $existing as api.get "https://api.example.com/users"
  if $existing == null
    api.post "https://api.example.com/users" {"name": "Alice", "email": "alice@example.com"}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step API workflow

Chain multiple api operations together.

@desc "Setup authentication"
do
  api.setAuth $token
enddo

@desc "Get, post, and more"
do
  set $r_get as api.get "https://api.example.com/users"
  set $r_post as api.post "https://api.example.com/users" {"name": "Alice", "email": "alice@example.com"}
  set $r_put as api.put "https://api.example.com/users/1" {"name": "Bob"}
  print "All operations complete"
enddo

5. Safe get with validation

Check results before proceeding.

@desc "Setup authentication"
do
  api.setAuth $token
enddo

@desc "Get and validate result"
do
  set $result as api.get "https://api.example.com/users"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/api

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size6.9 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web