HTTP API

Authentication / Account Management

Login STEP 1

Create Auth TokenPost/me

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 Step 2.

Example URI

Post https://admin.pubnub.com/api/me
Request
HideShow
Headers
Content-Type: application/json
Body
{
    "email": "blocksUser@pubnub.com",
    "password": "verySecretPassword"
}
Response  200
HideShow
Body
{
    result: {
        created: 1464063983,
        expires: 1467594357,
        modified: 1465002357,
        token: "39gjjhy5d-f713-4a02-b1ca-17a258f5c9cc",
        role: "user",
        status: 1,
        storage: null,
        user:{"created": 1400972126, "email": "...", "id": 123456, "login_attempts": 0,…},
        user_id: 123456,
        user_roles:{"is_internal_admin": false, "is_internal_operator": false, "is_internal_viewer": false, "roles":{}…}
    }
}
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -d '{
        "email": "blocksUser@pubnub.com",
        "password": "verySecretPassword"
    }' \
    -X POST \
    "https://admin.pubnub.com/api/me"

Login Step 2

Fetch Account IDGet/accounts?user_id=123456

This endpoint returns an array of accounts you are part of. Array item 0 is the account you are owning. That object has an ID field. You need to use this ID to in query apps and keys endpoint where owner_id = account.id

Example URI

Get https://admin.pubnub.com/api/accounts?user_id=123456
URI Parameters
HideShow
user_id
number (required) Example: 123456

use user_id received from Step 1.

Request
HideShow
Headers
Accept: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
HideShow
Body
{
    "result": {
        "accounts": [{
            "created": 1355425945,
            "dates": {
                "company": { 
                    "created": 1489708540, 
                    "modified": null
                }
            },
            "id": 267180,
            "modified": null,
            "owner_id": 123456,
            "properties": { 
                "company": "home"
            },
            "protected": 0
        }],
        "permissions": {}
    }
}
Example
HideShow
curl \
    -H "Accept: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X GET \
    "https://admin.pubnub.com/api/accounts?user_id=123456"

Listing Applications

Listing ApplicationsGet/apps?owner_id=267180

Example URI

Get https://admin.pubnub.com/api/apps?owner_id=267180
URI Parameters
HideShow
owner_id
number (required) Example: 267180

return Keys for this application.

Request
HideShow
Headers
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
HideShow
Example
HideShow
curl \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X GET \
    "https://admin.pubnub.com/api/apps?owner_id=267180"

Block Operations

Show All

Listing Blocks GET/v1/blocks/key/178088/block

Example URI

GET https://admin.pubnub.com/api/v1/blocks/key/178088/block
URI Parameters
HideShow
keyId
number (required) Example: 178088

key id for which to fetch associated blocks

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X GET \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block"

Starting

StartingPOST/v1/blocks/key/178088/block/71/start

Example URI

POST https://admin.pubnub.com/api/v1/blocks/key/178088/block/71/start
URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the block.

blockId
number (required) Example: 71

block id to start.

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X POST \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block/71/start"

Stopping

StoppingPOST/v1/blocks/key/178088/block/71/stop

Example URI

POST https://admin.pubnub.com/api/v1/blocks/key/178088/block/71/stop
URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the block.

blockId
number (required) Example: 71

block id to stop.

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X POST \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block/71/stop"

Event Handlers

Show All

Listing Event HandlersGET/v1/blocks/key/178088/block/71

Example URI

GET https://admin.pubnub.com/api/v1/blocks/key/178088/block/71
URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the block.

blockId
number (required)  Example: 71

block id to list event handlers for.

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X GET \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block/71"

Creating Event Handler

  1. before-publish handlerPOST/v1/blocks/key/178088/event_handler

    Be sure to reformat the code before uploading using the following snippet:

    EH_FILE="event-handler.js"
      EVENT_HANDLER='cat $EH_FILE | sed "s/'/\\\'/g" | sed 's/"/\\\"/g' | sed 's/[\r\n]/\\n/g''

    Example URI

    POST https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler
    URI Parameters
    HideShow
    keyId
    number (required) Example: 178088

    key which owns the event handler.

    Request
    HideShow
    Headers
    Content-Type: application/json
    X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
    Body
    
    {
        "type": "before-publish",
        "key_id": 178088,
        "block_id": 71,
        "channels": "eventHandlerChannel",
        "code": "console.log('hi')",
        "event": "js-before-publish",
        "log_level": "debug",
        "name": "eventHandlerName",
        "output": "output-0.5823105682419438"
    }
    

    Note: In output you can pass unique string and use it as the output channel. The function will publish to it and the portal client will subscribe to it.

    Response  200
    Example
    HideShow
    curl \
        -H "Content-Type: application/json" \
        -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
        -d '{"type": "before-publish", "key_id": 178088, "block_id": 71, "channels": "eventHandlerChannel", "code": "console.log(\u0027hi\u0027)", "event": "js-before-publish", "log_level": "debug", "name": "eventHandlerName", "output": "output-0.5823105682419438"}' \
        -X POST \
        "https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler"
    
  2. after-publish handlerPOST/v1/blocks/key/178088/event_handler

    Be sure to reformat the code before uploading using the following snippet:

    EH_FILE="event-handler.js"
      EVENT_HANDLER='cat $EH_FILE | sed "s/'/\\\'/g" | sed 's/"/\\\"/g' | sed 's/[\r\n]/\\n/g''

    Example URI

    POST https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler
    URI Parameters
    HideShow
    keyId
    number (required) Example: 178088

    key which owns the event handler.

    Request
    HideShow
    Headers
    Content-Type: application/json
    X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
    Body
    
    {
        "type": "after-publish",
        "key_id": 178088,
        "block_id": 71,
        "channels": "eventHandlerChannel",
        "code": "console.log('hi')",
        "event": "js-after-publish",
        "log_level": "debug",
        "name": "after-publish-eventHandlerName",
        "output": "output-0.5823105682419438"
    }
    

    Note: In output you can pass unique string and use it as the output channel. The function will publish to it and the portal client will subscribe to it.

    Response  200
    Example
    HideShow
    curl \
        -H "Content-Type: application/json" \
        -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
        -d '{"type": "after-publish", "key_id": 178088, "block_id": 71, "channels": "channelName", "code": "console.log('hi');", "event": "js-after-publish", "name": "get-kv-state", "output": "output-0.5823105682419438"}' \
        -X POST \
        "https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler"
    
  3. REST Endpoint handlerPOST/v1/blocks/key/178088/event_handler

    This creates an endpoint at https://pubsub.pubnub.com/v1/blocks/sub-key/mysubkey/some/path/here

    Example URI

    POST https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler
    URI Parameters
    HideShow
    keyId
    number (required) Example: 178088

    key which owns the event handler.

    Request
    HideShow
    Headers
    Content-Type: application/json
    X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
    Body
    
    {
        "type": "on-rest",
        "key_id": 178088,
        "block_id": 71,
        "channels": "eventHandlerChannel",
        "code": "console.log('hi')",
        "event": "js-on-rest",
        "log_level": "debug",
        "name": "on-rest-eventHandlerName",
        "path": "some/path/here",
        "output": "output-0.5823105682419438"
    }
    

    Note: In output you can pass unique string and use it as the output channel. The function will publish to it and the portal client will subscribe to it.

    Response  200
    Example
    HideShow
    curl \
        -H "Content-Type: application/json" \
        -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
        -d '{"type": "on-rest", "key_id": 178088, "block_id": 71, "channels": "", "code": "some javascript;", "path": "some/path/here", "event": "js-on-rest", "name": "on-rest-eventHandlerName", "output": "blocks-output-ILBHlac94cJJAfHUb6xI.417330032733467"}' \
        -X POST \
        "https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler"
    

Updating Event Handler

Updating Event HandlerPUT/v1/blocks/key/178088/event_handler/112

Be sure to reformat the code before uploading using the following snippet:

EH_FILE="event-handler.js"
  EVENT_HANDLER='cat $EH_FILE | sed "s/'/\\\'/g" | sed 's/"/\\\"/g' | sed 's/[\r\n]/\\n/g''

Example URI

PUT https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler/112
URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the event handler.

eventHandlerId
number (required) Example: 112

id of event handler to be updated

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Body
{
    "type": "before-publish",
    "key_id": 178088,
    "block_id": 71,
    "event_handler_id": 112,
    "channels": "eventHandlerChannel",
    "code": "console.log('hi')",
    "event": "js-before-publish",
    "log_level": "debug",
    "name": "eventHandlerName",
    "output": "output-0.5823105682419438"
}
Response  200
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -d '{"type": "before-publish", "key_id": 178088, "block_id": 71, "event_handler_id": 112, "channels": "eventHandlerChannel", "code": "console.log(\u0027hi\u0027)", "event": "js-before-publish", "log_level": "debug", "name": "eventHandlerName", "output": "output-0.5823105682419438"}' \
    -X PUT \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/event_handler/112"

Block Creation

CREATE Block

Create BlockPOST/api/v1/blocks/key/<key_id>/block

URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the event handler.

blockId
number (required) Example: 71

block id to list event handlers for.

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Body
{
"key_id": < key id>,
"name": "< name of the block>",
"description": "< description of the block>"
}
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -d '{"key_id": 178088,"name": "new block","description": "new block"}' 
    -X POST \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block"

UPDATE Block

UPDATE BlockPUT/api/v1/blocks/key/<key_id>/block/<block_id>

URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the event handler.

blockId
number (required) Example: 71

block id to list event handlers for.

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Body
{
"key_id": < key id>,
"name": "< name of the block>",
"description": "< description of the block>"
}
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -d '{"key_id": 178088,"name": "new block","description": "new block"}' \
    -X PUT \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block/71"

DELETE Block

DELETE BlockDELETE/api/v1/blocks/key/<key-id>/block/<block-id>

URI Parameters
HideShow
keyId
number (required) Example: 178088

key which owns the event handler.

blockId
number (required) Example: 71

block id to list event handlers for.

Request
HideShow
Headers
Content-Type: application/json
X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc
Response  200
Example
HideShow
curl \
    -H "Content-Type: application/json" \
    -H "X-Session-Token: 394f7dbd-f713-4a02-b1ca-17a258f5c9cc" \
    -X DELETE \
    "https://admin.pubnub.com/api/v1/blocks/key/178088/block/71"