this post was submitted on 14 Jun 2023
30 points (100.0% liked)

Programming

13382 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 2 years ago
MODERATORS
 

I'm looking for examples of calls to the Lemmy API.. I've been to the following link in the documentation:

https://join-lemmy.org/docs/en/contributors/04-api.html

However I don't see any direct examples of uses of the API for common cases, like creating a post, creating a comment or getting either type of item. Some of the linked documentation from that page points to what I believe is typescript code for interfaces, but that does not really have examples of actually calling those interfaces. I can make some logical guesses at to what the calls should be, but I don't have a way to really verify this yet.

Does anyone have some working examples they can post?

top 10 comments
sorted by: hot top controversial new old
[–] megaman1970@lemmy.world 10 points 1 year ago (1 children)

Okay, before I head off to bed, I think this works for the login and authentication token:

import requests
import json

def login(username_or_email, password):
    # Define the URL for the login endpoint
    url = "https://lemmy.ml/api/v1/user/login"

    # Define the headers for the request
    headers = {'Content-Type': 'application/json'}

    # Define the data for the login
    data = {
     "username_or_email": username_or_email,
     "password": password
    }

    # Send the POST request
    response = requests.post(url, headers=headers, data=json.dumps(data))

    # Extract the JWT from the response
    jwt = response.json().get('jwt')

    return jwt

# Use the login function
jwt = login("your_username_or_email", "your_password")
print(jwt)

The JSON Web Token (jwt) should contain the authentication token. At least I think that's the case. I picked this out by reading through the Go code at the following URL: https://github.com/Elara6331/go-lemmy/blob/master/lemmy.go

I'll play around with the code later.

[–] ANapSoundsNice 6 points 1 year ago* (last edited 1 year ago) (1 children)

Sorry for the late response to my other comment - I also was reading through the documentation for the first time and it looks like you got the answer ahead of me, nice!

I whipped up some sample code that does exactly the same thing you ended up doing, so no further additions here except that in the Lemmy API is expecting requests to be sent to <instance domain>/api/v3/... .

I used my code that is basically the same to what you have above here, but when I switched it to v1 the server throws an 400 error (malformed request). So if you haven't ran this code already you've got my sanity check that it will work except for making sure you change the api version. You can then carry that auth token with you when making requests by including it in the header like so

headers = {
  'Content-Type': 'application/json'
  'auth': '<jwt goes here>'
}
[–] megaman1970 2 points 1 year ago

Thanks, I'll revamp my code when I start testing it later. I think eventually I'll put together a python library for interacting with Lemmy, or at least give enough of an example that someone else can get a good start.

[–] ta_leadran_orm@lemmy.world 9 points 1 year ago

There's a typescript client here, you could look at that for examples, I can't see any dedicated API documentation anywhere https://github.com/LemmyNet/lemmy-js-client/tree/main/src

See more details in this issue here https://github.com/LemmyNet/lemmy/issues/2937

[–] megaman1970 5 points 1 year ago* (last edited 1 year ago) (1 children)

Here's a kind of guess on how to create a post using python's requests library:

import requests
import json

# Define the URL for the API endpoint
url = "https://lemmy.ml/api/v1/post"

# Define the headers for the request
headers = {'Content-Type': 'application/json'}

# Define the data for the new post
data = {
 "name": "Your Post Title",
 "community_id": 123,  # Replace with your community ID
 "url": "https://your-url.com",  # Optional
 "body": "Your post content",  # Optional
 "nsfw": False,  # Optional
 "language_id": 1,  # Optional, replace with your language ID
 "auth": "your_auth_token_here"
}

# Send the POST request
response = requests.post(url, headers=headers, data=json.dumps(data))

# Print the response
print(response.json())

Does this look right? I understand that I'll have to authenticate to the server to do this, but I'm really not sure how to do that.

[–] ANapSoundsNice 4 points 1 year ago (1 children)

Very jank response because I’m on my phone!

Looks like you’ll need to POST the header Auth with the token you receive as a LoginResponse https://join-lemmy.org/api/interfaces/LoginResponse.html

(Brb for an edit)

[–] megaman1970@lemmy.world 1 points 1 year ago

Cool. Then my other answer is correct. I appreciate the help!

[–] netwren@lemmy.world 4 points 1 year ago

If Lemmy doesn't have it the community could open source a swagger doc for the api

[–] agressivelyPassive@feddit.de 2 points 1 year ago

If someone has a write-up of Lemmy's architecture, I would also be interested.

I don't have the time or expertise to sift through the rust code.

[–] megaman1970@lemmy.world 1 points 1 year ago

Here's another example, this time for creating a comment:

import requests
import json

# Define the URL for the API endpoint
url = "https://lemmy.ml/api/v1/comment"

# Define the headers for the request
headers = {'Content-Type': 'application/json'}

# Define the data for the new comment
data = {
 "content": "Your comment content",
 "post_id": 123,  # Replace with the ID of the post you're commenting on
 "form_id": "your_form_id",  # Replace with your form ID
 "auth": "your_auth_token_here"
}

# Send the POST request
response = requests.post(url, headers=headers, data=json.dumps(data))

# Print the response
print(response.json())

Does anyone know how to do the login process in Lemmy, and retrieve an auth token?