Getting started with MediaLit's REST API

May 18, 2024

Using MediaLit in your app is as easy as consuming any REST endpoint. You do not have to install anything.

Before you begin

You need to create an app and obtain an API key to upload the file.

Read how to do that in 2 minutes here.

1. Uploading files

To upload a file, simply send an HTTP request to the following REST endpoint along with the required data.


Endpoint

POST http://api.medialit.cloud/media/create

Payload

Add the form data to your request, containing the following properties.

  1. apikey (type: string) (required): To authenticate your request.

  2. file (type: file) (required): The actual file.

  3. access (type: true|false) (optional): If the file is open to everyone or not. If you do not specify this property, the uploaded file will be private i.e. requires authorization to access it.

  4. caption (type: string) (optional): A caption for the file

  5. group (type: string) (optional): A discreet string you would like to attach to the file. This comes handy for grouping files on your application side.

If the request goes through, you will get a response like the following.

{
    "mediaId": "iEZYLi_hy",
    "originalFileName": "some-file.png",
    "mimeType": "image/png",
    "size": 10240,
    "access": "private",
    "file": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/main.png",
    "thumbnail": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/thumb.webp"    
}

Understanding the returned response.

There are a few details you would like to understand about the returned response.

  1. mediaId: Every uploaded file on MediaLit gets a unique id which uniquely identifies the file on the service. You will use this id to perform actions on the file.

  2. access: If the file is open to everyone.

  3. size: Total file size in bytes.

  4. file: The direct URL to the file.

    In case of public files i.e. (the access is public), this can be re-used. However, in case of private files (the access in private) this URL needs to be generated everytime you want to show/access the file.

  5. thumbnail: A direct URL to the generated thumbnail for the file. This will always be public and can you saved by your app.

MediaLit does not allow updating details of the uploaded file later. It is to keep the objects immutable so that the things don't start breaking in your apps if you update a file.

Do not want to expose your API key to the end user?

MediaLit offers an alternate way to generate a unique one-time upload URL which you can hand over the client (or your frontend) and they can upload data directly to MediaLit, without any intermediate server.

You can use this mechanism to generate the unique upload links on the server side, where you have access to the API key.

To obtain the unique one-time upload link, send an HTTP request to the following endpoint.

Endpoint

POST http://api.medialit.cloud/media/presigned/create

Headers

Content-Type: application/json

Payload

{
	"apikey": "your API key"
}

If the request goes through, you will get a response like the following.

{
    "message": "https://api.medialit.cloud/media/create?signature=I7Mr5htp5sN6s43PfXeEaFVMR1Pc"
}

Your client (or the frontend app) can use this URL to upload a file. It accepts all parameters supported by the previous /media/create endpoint, except apikey.

2. Accessing a specific file

You can access any file by using its mediaId.

To obtain the file's details, send an HTTP request to the following endpoint. Just replace the <media-id> with the actual media id.

POST https://api.medialit.cloud/media/get/<media-id>

Headers

Content-Type: application/json

Payload

{
	"apikey": "your API key"
}

If the request goes through, you will get a response like the following.

{
    "mediaId": "iEZYLi_hy",
    "originalFileName": "some-file.png",
    "mimeType": "image/png",
    "size": 10240,
    "access": "private",
    "file": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/main.png",
    "thumbnail": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/thumb.webp"  
}

3. Access all files

You can access a paginated list of all of your files.

To get this, send an HTTP request to the following endpoint.

POST https://api.medialit.cloud/media/get

Headers

Content-Type: application/json

Payload

{
	"apikey": "your API key"
}

If the request goes through, you will get a response like the following.

[
 {
    "mediaId": "iEZYLi_hy",
    "originalFileName": "some-file.png",
    "mimeType": "image/png",
    "size": 10240,
    "access": "private",
    "file": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/main.png",
    "thumbnail": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/thumb.webp"  
 },
 {
    "mediaId": "iEZYLi_hy2",
    "originalFileName": "some-file2.png",
    "mimeType": "image/png",
    "size": 10240,
    "access": "private",
    "file": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/main.png",
    "thumbnail": "https://d27g932tzd9f7s.cloudfront.net/medialit-service/public/b-H8rrVnm3t5fTQy9OJ9T7iq3X6UKCD2xmjXwuio/thumb.webp"  
 }
]

3. Delete a file

To delete a file, send an HTTP request to the following endpoint. Just replace the <media-id> with the actual media id.

DELETE https://api.medialit.cloud/media/delete/<media-id>

Headers

Content-Type: application/json

Payload

{
	"apikey": "your API key"
}

If the request goes through, you will get the following response.

{
    "message": "success"
}

That's it! It is that easy.


Have more questions?

Feel free to reach out to us, using the chatbot provided here or through our Discord. We are happy to help you out.