#!/bin/bash

# Basic login script for Lemmy API

# CHANGE THESE VALUES
my_instance=""			# e.g. https://feddit.nl
my_username=""			# e.g. freamon
my_password=""			# e.g. hunter2

########################################################

# Lemmy API version
API="api/v3"

########################################################

# Turn off history substitution (avoid errors with ! usage)
set +H

########################################################

# Login
login() {
	end_point="user/login"
	json_data="{\"username_or_email\":\"$my_username\",\"password\":\"$my_password\"}"

	url="$my_instance/$API/$end_point"

	curl -H "Content-Type: application/json" -d "$json_data" "$url"
}

login

# Make note of "jwt" from reply

This'll reply with JSON data, that includes a value for "jwt", to be used in other scripts that require you to be logged in.

Personally, I'd use the 'jq' program to de-serialize the JSON data, but I thought I'd keep the script simple for now

  • Ken Oh@lemm.ee
    ·
    edit-2
    1 year ago

    Great. Now how would I make a post? Is there an API endpoint list somewhere?

    I wanted to schedule my community posts, and tried to use this: https://github.com/RikudouSage/LemmySchedule but it doesn't actually make posts for me. If I could use stuff like the above I could crontab it all out.

    EDIT: Hmmm, looks like I found it https://join-lemmy.org/api/interfaces/CreatePost.html Now to test and figure it all out.