REST Javascript Login (or API Key Generation)?

Hi all,
i’m embedding the possibility to post discourse’s topic from external site through javascript. For this i need to generate an user’s api key, but if user isn’t logged on discourse is needed to shown a login form to do it. Only after the login (javascript) creates the new topic through REST.
Anyone can help me?