REST API question
-
I am trying to upload files via code, and then use the REST API to make the ‘connection’ between the file and WPDM.
Uploading the files to the proper folder (download-manager-files, no subfolders) is not a problem.
However, when I create a package via the REST API, using the same file name as a parameter, it creates an empty package. In other words, the package exists in WPDM, it has the right name, but there is no attached file.
Any ideas what I am doing wrong? In the REST API docs/examples I cannot find any mention of the file itself in the POST part, only in the JSON return.
Thanks, Martijn
-
Hello @martijnrammeloo,
Hope you are well. The issue is that you cannot simply pass the filename as a string parameter. The files parameter expects a specific object structure with numeric keys as file IDs.
What You’re Probably Doing Wrong (incorrect)
{
"title": "My Package",
"files": "myfile.pdf"
}This won’t work because files expects an object, not a string.
The Correct Way
When you’ve already uploaded files to the
download-manager-filesfolder, you must pass the files parameter as an object with
numeric keys:{
"title": "The Alchemist",
"description": "Package description",
"status": "publish",
"files": {
"1": "The Alchemist.pdf"
}
}Key points:
- The key (“1”) is the file ID (use “1”, “2”, etc. for multiple files)
- The value is just the filename – not the full path
- Files must already exist in
wp-content/uploads/download-manager-files/Multiple Files Example{
"title": "My Package",
"status": "publish",
"files": {
"1": "file1.pdf",
"2": "file2.pdf",
"3": "file3.zip"
}
}
Alternative: Upload Files Directly via REST API Instead of pre-uploading, you can upload files when creating the package usingmultipart/form-data: curl -X POST \
https://yoursite.com/wp-json/wpdm/v1/packages \
-H 'Authorization: Bearer YOUR_API_KEY' \
-F "title=My Package" \
-F "status=publish" \
-F "files=@/path/to/your/file.pdf"
Documentation: https://imsas.github.io/wpdm-rest-api-docs
Please kindly check.Thank you
Thank you for your response, it is really helpful!
In PHP, I now have the following code:
$params = [
“title” => $file_name,
“status” => “publish”,
“files” => [“1” => “k3.pdf”],
];(“k3.pdf” is hard-coded, only for testing purposes)
Executing this code gives no errors, creates a package, but still does not attach the file to the package, even though it resides in the proper folder. The API returns a string by the way, so that does not seem right: [“files”]=> string(6) “k3.pdf”. So, I suspect that something is wrong with my format.
I am also not sure what you mean with ‘The key (“1”) is the file ID (use “1”, “2”, etc. for multiple files)’. Do you mean that the file must already have an ID in WordPress? Or, do you mean that I must use a different key for each file that I upload?
I think I am almost there, but I must be missing something really obvious.Martijn
-
This reply was modified 4 months, 1 week ago by
martijnrammeloo.
-
This reply was modified 4 months, 1 week ago by
martijnrammeloo.
Hello @martijnrammeloo,
Your
$paramsarray format is correct, but the issue is likely in how you’re
sending the request to the API.Understanding the Issue
Your code:
$params = [
"title" => $file_name,
"status" => "publish",
"files" => ["1" => "k3.pdf"],
];This array setup is correct according to the documentation (see: https://imsas.github.io/wpdm-rest-api-docs/). However, this is
only the data structure – you also need to send it properly to the API.The Missing Piece: How to Send the Request
Your code is missing the part where you actually send this to the REST API. Here’s the complete working code:
Method 1: Pre-Uploaded Files (via JSON)
// Your API key from
WordPress Admin > WPDM Settings > REST API$api_url = 'https://yoursite.com/wp-json/wpdm/v1/packages';
$api_key = 'get-this-from-wpdm-settings'; // See below for how to get this$params = [
"title" => $file_name,
"status" => "publish",
"files" => ["1" => "k3.pdf"], // Your existing code - this is correct!
];// THIS IS THE MISSING PART – properly send the request:
$response = wp_remote_post(
$api_url,
[
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json', // CRITICAL!
],
'body' => json_encode($params), // CRITICAL!
'timeout' => 45,
]
);// Check the result
if (is_wp_error($response)) {echo 'Error: ' . $response->get_error_message();
} else {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);if (isset($data['id'])) {
echo "Package created! ID: " . $data['id'];
} else {
echo "Response: " . $body;
}
}What was likely missing:
- Content-Type:
application/jsonheader json_encode($params)in the body
cURL Version (Method 1)
curl -X POST \
https://yoursite.com/wp-json/wpdm/v1/packages \
-H 'Authorization: Bearer YOUR_API_KEY_FROM_SETTINGS' \
-H 'Content-Type: application/json' \
-d '{
"title": "My Package",
"status": "publish",
"files": {
"1": "k3.pdf"
}
}'Where to Get Your API Key
The documentation says: “
Authentication is done via the API key which you can find in your WPDM REST API settings.“To get your API key:
- Go to WordPress Admin Dashboard
- Navigate to WordPress
Download Manager > Settings - Find the REST API tab/section
- Copy your API Key (auto-generated, you can regenerate it) Note: The documentation examples use a sample key like 613efe9acb0d5c2b – replace this with your actual key from settings.
File Location Requirements
Your file k3.pdf must exist in:
/wp-content/uploads/download-manager-files/k3.pdfVerify this by checking
WPDM Settings > File Managerfor the exact upload path.About “File ID” – Clarification
When I mentioned keys like “1”, “2”, “3” – these are NOT WordPress file IDs. They’re simply sequential numeric string keys to
identify each file. Think of them as “file #1”, “file #2”, etc. You don’t need to retrieve any ID from WordPress.Documentation: https://imsas.github.io/wpdm-rest-api-docs/
-
This reply was modified 4 months, 1 week ago by
Nayeem Hyder.
-
This reply was modified 4 months, 1 week ago by
Nayeem Hyder.
-
This reply was modified 4 months, 1 week ago by
Nayeem Hyder.
YES!
The json_encode part was missing on my side. Thanks a lot!
Glad to hear that. However, if you need further help with anything else, then please don’t hesitate to open a new topic. If you get some free moments, can you please give us a 5* here https://ww.wp.xz.cn/support/plugin/download-manager/reviews/?rate=5#new-post, It will inspire us a lot. Thanks in advance
Thank you again and regards
You must be logged in to reply to this topic.