Stephan Michard
Stephan Michard

Reputation: 33

Matrix / Synapse: send image via client server API

Matrix is an open network for secure, decentralized communication. I run the Synapse implementation of the Matrix Homeserver on one of my hosts.

I would like to send messages using the client-server API to a specific matrix room. I use an access token and a room id to perform API calls. I can send text messages with the following curl command:

curl --header "Authorization: Bearer my_token" -X POST -d '{"msgtype":"m.text", "body":"my hello world messsage"}' "https://my_homeserver_url/_matrix/client/r0/rooms/"my_room_id"/send/m.room.message"

Unfortunately, I have not yet been able to send an image that is located locally on the computer via the client-server API. According to the documentation you have to select m.image as the message type and reference the image accordingly.

Unfortunately, even after intensive research, I haven't found a working example yet. Can someone point me in the right direction?

I have tried various curl commands and tried to reference the file, either by a link, a path, or a pipe command.

Upvotes: 2

Views: 3231

Answers (1)

clamentjohn
clamentjohn

Reputation: 4017

You have to upload the file / image and then attach the file's url to a new event. Here is an example:

1. Uploading the image - link to the spec

curl --data-binary @image.png 'https://matrix.server/_matrix/media/v3/upload?filename=image.png' \
  -X 'POST' \
  -H 'Authorization: Bearer my_access_token' \
  -H 'Content-Type: image/png' \
  --compressed

You'll get a response like this

{
  "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw"
}

2. Sending an event - link to the spec

curl 'https://matrix.server/_matrix/client/v3/rooms/!rVwmJkYsikYjnIeLNU:matrix.server/send/m.room.message/1212' \
  -X 'PUT' \
  -H 'Authorization: Bearer my_access_token' \
  -H 'accept: application/json' \
  -H 'content-type: application/json' \
  --data-raw '{"info":{"mimetype":"image/png","size":512,"w":512,"h":512},"msgtype":"m.image","body":"tta.webp","url":"mxc://example.com/AQwafuaFswefuhsfAFAgsw"}' \
  --compressed

Be aware that the transaction ID (1212 in the example above) needs to be unique for your access token. So using an uuid (e.g. generated with uuidgen) might be a good idea.

Upvotes: 2

Related Questions