Getting notifications through API


(Clifford Duke) #1

I was looking through the API and found that the notifications comes from https://meta.discourse.org/notifications.json

I was wondering if there was any way to get this based on the user if a master API key is provided?

And is there a way to create custom notifications from the API?

What I am hoping to accomplish is show the users own notifications in my main site using their username and also post notifications to it if something happens on the main site.


(Kane York) #2

Please ignore while I rewrite this, I misread the post…

This is definitely possible if you add a CORS header.


(Nilesh Trivedi) #3

/users/:username/notifications is just an alias to users#show which returns notification_count but not the actual notification messages. An API to read notifications for any user will be super helpful in building Discourse a seamless component of a website/app.


(Kane York) #4

There is a way to get it now:

/notifications/history.json?user=username

Requires api_username to be staff.


(Mittineague) #5

That gives me 60 of them - goes back to yesterday - but good enough for now for my use.
Thanks


(Kane York) #6

Go to https://meta.discourse.org/my/notifications and watch the Network panel when it infinite scrolls for how to go back farther.

edit: it’s ?before=timestamp


(Grzegorz Majerski) #7

Is it still possible to get notifications based on username through api in discourse ?
I’m using version v1.6.0.beta11 +104

/notifications/history.json
endpoint doesn’t seem to work anymore

/notifications.json
only returns notifications for ‘system’ user
Additional parameters beside api_key are also not recognized
I’ve tried:

&api_username=username
&user=username
&api_username=staff&user=username

(Kane York) #8

Looks like it changed. Here’s what ‘history’ is now:

https://meta.discourse.org/notifications.json?username=Grzegorz_Majerski

(Grzegorz Majerski) #9

Thanks for your reply.

Unfortunately username parameter also doesn’t work when accessing api with external service using API key
Whether I use username parameter or not, the response stays almost the same. In both cases I get notifications for “system” user and the only difference is additional message stating that username parameter is not recognized when provided

here are example curl request and responses

  • without username parameter

curl http://my.discourse.com/notifications.json?api_key=1234567890

{  
   "notifications":[  
      {  
         "id":1,
         "notification_type":12,
         "read":false,
         "created_at":"2016-07-19T11:10:31.429Z",
         "post_number":null,
         "topic_id":null,
         "slug":null,
         "data":{  
            "badge_id":41,
            "badge_name":"First Emoji",
            "badge_slug":"first-emoji",
            "username":"system"
         }
      }
   ],
   "total_rows_notifications":1,
   "load_more_notifications":"/notifications?offset=60&username=system"
}
  • with username parameter

curl http://my.discourse.com/notifications.json?api_key=1234567890&username=Grzegorz_Majerski

{  
   "notifications":[  
      {  
         "id":1,
         "notification_type":12,
         "read":false,
         "created_at":"2016-07-19T11:10:31.429Z",
         "post_number":null,
         "topic_id":null,
         "slug":null,
         "data":{  
            "badge_id":41,
            "badge_name":"First Emoji",
            "badge_slug":"first-emoji",
            "username":"system"
         }
      }
   ],
   "total_rows_notifications":1,
   "load_more_notifications":"/notifications?offset=60&username=system"
}'username' is not recognized as an internal or external command, operable program or batch file.

Can user notifications be retrieved when accessing API this way ?


(Mittineague) #10

Did you try username_lower for the username value? i.e.

username=grzegorz_majerski

(Grzegorz Majerski) #11

I did, the result stays the same


(Kane York) #12

You need to put quotes around the URL - the username parameter was cut off from it.

curl 'http://my.discourse.com/notifications.json?api_key=1234567890&username=Grzegorz_Majerski'

(Grzegorz Majerski) #13

that was the case all along, how embarassing

thank you Kane