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?

  • megaman1970@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    1 year ago

    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.

  • megaman1970@beehaw.orgOP
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 year ago

    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?

  • AggressivelyPassive@feddit.de
    link
    fedilink
    arrow-up
    2
    ·
    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.