Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active April 8, 2024 15:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/5acf10d2691fbb26a50a5f51e8547967 to your computer and use it in GitHub Desktop.
Save stephenlb/5acf10d2691fbb26a50a5f51e8547967 to your computer and use it in GitHub Desktop.
PubNub Functions REST APIs using CURL Commands

PubNub Functions REST APIs using CURL Commands

PubNub Functions

Authenticate with an email and password to receive a session_token. The Session Token will be a top level key called token. The Session Token token is used for all requests after authentication. You also receive a user_id which is used in your apps and keys lookup.

Login

curl --data '{"email":"<email>","password":"<password>"}'       \
    -H 'Content-Type: application/json'                         \
    https://admin.pubnub.com/api/me

List Apps and Keys

curl https://admin.pubnub.com/api/apps?owner_id=<user_id>  \
    -H 'X-Session-Token: <session_token>'

List Keys by App ID optional

curl https://admin.pubnub.com/api/keys?app_id=<app_id>     \
    -H 'X-Session-Token: <session_token>'

Functions

curl https://admin.pubnub.com/api/v1/blocks/key/<key_id>/block  \
    -H 'X-Session-Token: <session_token>'

Event Handlers

curl https://admin.pubnub.com/api/v1/blocks/key/<key_id>/block/<block_id>  \
    -H 'X-Session-Token: <session_token>'

Update Event Handlers

EH_FILE="event-handler.js"
EVENT_HANDLER=`cat $EH_FILE | sed "s/'/\\\'/g" | sed 's/"/\\\"/g' | sed 's/[\r\n]/\\n/g'`
curl --data '{"key_id":<key_id>,"block_id":<block_id>,"id":<event_handler_id>,"channels":"<channel>","code":"$EVENT_HANDLER","event":"js-before-publish","log_level":"debug","name":"New Event Handler","output":"output-0.5823105682419438"}' \
    -X PUT                                                                                      \
    https://admin.pubnub.com/api/v1/blocks/key/<key_id>/event_handler/<event_handler_id>  \
    -H 'X-Session-Token: <session_token>'

Start Event Handler

curl --data '{"block_id":<block_id>,"key_id":<key_id>,"action":"start"}'  \
    -H 'Content-Type: application/json'                                   \
    -H 'X-Session-Token: <session_token>'                                 \
    https://admin.pubnub.com/api/v1/blocks/key/<key_id>/block/<block_id>/start

Stop Event Handler

curl --data '{"block_id":<block_id>,"key_id":<key_id>,"action":"stop"}'  \
    -H 'Content-Type: application/json'                                  \
    -H 'X-Session-Token: <session_token>'                                \
    https://admin.pubnub.com/api/v1/blocks/key/<key_id>/block/<block_id>/stop

Create Event Handler

EH_FILE="event-handler.js"
EVENT_HANDLER=`cat $EH_FILE | sed "s/'/\\\'/g" | sed 's/"/\\\"/g' | sed 's/[\r\n]/\\n/g'`
curl --data '{"key_id":<key_id>,"block_id":<block_id>,"id":<event_handler_id>,"channels":"<channel>","code":"$EVENT_HANDLER","event":"js-before-publish","log_level":"debug","name":"New Event Handler","output":"output-0.5823105682419438"}' \ 
    -H 'Content-Type: application/json'                                                         \
    -H 'X-Session-Token: <session_token>'                                                       \
     https://admin.pubnub.com/api/v1/blocks/key/<key_id>/event_handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment