I am using the Netlify API from within a serverless Node.js Google Cloud function. I have successfully gotten both .zip file deployment as well as file digest deployment working and it’s great!
The only problem I’m having is that I cannot get the digest for the functions to work…
My function has 0 third-party dependencies and the only import is the native https module.
I’m using zip-a-folder 3rd party library to perform the zip operation.
This is immensely frustrating because the OpenAPI spec and docs offer no real-world example of deploying functions through the API. In fact, one must refer to both the OpenAPI schema as well as this additional document here Deploy functions | Netlify Docs just to know that it has to be a zip file.
When I send the functions digest in the request body I just get back a deploy response with NO required_functions and then a 404 when I try to upload the function itself.
It seems many others have had difficulty with this so can you please make an example request for this.
Since the zip-it-and-ship-it is ESM only, I cannot use it in my code and I also do not think this is the issue.
It’s frustrating that someone had a long thread and after multiple back and forth the docs were barely updated with only a sentence or two and it took the support team 10 months to reply. I do not have 10 months to wait as we are launching a product on Netlify and need this working ASAP as advertised in the documentation.
But I don’t get required_functions in the digest, it appears this functionality is broken. I have spent over 24 hours on this so I’d really appreciate a reply, I’m on the Pro plan.
It appears that the endpoint https://api.netlify.com/api/v1/sites does not work for uploading functions. This really should be documented in the API / docs since this link says you can send the digest directly when you create a site to avoid two HTTP requests.
In fact, this doesn’t work at all for functions as I previously noted. It appears to only work in a two step process of first creating the site and then creating a deploy. If someone had simply documented this, I could have saved hours of my own time, and avoided opening a support ticket. As a software engineer… very annoying.
I can’t confirm it is working right away but I am seeing the required_functions key in the response now.
Ok, after swapping to the two step process of creating a site and only then uploading the digest afterward… I try to upload the zip file of the required function. I’ve tried both in a (1) Postman request (2) Node.js axios request.
I’m getting a 500 Internal Server Error with the message unexpected status code from Functions Origin: 400
Due to your number of posts and your progress on this issue, the answer to your last response starts towards the end (you can find it with a bold heading). The rest of the post aims to clarify the rest of the things you’ve raised in this thread.
Points to address:
There are 2 problems in your setup. Please note that this could take a few back-n-forth, and if you’re not mentally prepared for this, it’s just going to lead to frustration on your end.
Problem #1 - Incorrect endpoint:
(you have figured it out towards the end, but I thought I’ll mention it for anyone else to find)
You’re using the wrong endpoint. The endpoint is https://api.netlify.com/api/v1/sites/site_id/deploys (documented here)
Problem #2 - Invalid zip:
Generating a correct zip is not as simple as just putting the JS/TS file inside a zip. Even if you’re not using any dependencies, the code needs to be bundled! Try using a simple code like (in ESM mode, with node_bundler as esbuild (as required for ESM code):
and using Netlify CLI to bundle it locally. You can then see the generated zip is not as simple. The deploy would probably work though even if you submit an invalid zip, but your function might fail to run.
However, if you’re using everything default (in CJS mode), it should work fine.
The Open API specification is auto-generated from a YAML file, I believe. I think it doesn’t offer a way to provide examples, though I could be wrong on that.
You can skip the Open API spec to read about API deploys. The Netlify Documentation has far more information.
Reason mentioned in Problem #1.
I had put a basic example here: Cant figure out how to make put request using fetch in javascript - Support - Netlify Support Forums, but I haven’t shown functions in there. Deploying manually via API and that too functions, is a very rarely-used use-case, so we (support team) did not invest much in writing Support Guides for it as opposed to much more common issues for which the Support Team has written vaious guides. This is also partly because, API deploys can be done in many ways. I can write a guide in JavaScript, but someone else can come saying they want in Python, someone might want in Java, etc. It’s not possible to do that. Even then, I’ll try to keep one guide ready when I get a chance.
Unfortunately, that is a part of your problem.
Please don’t provide wrong information. The Support team replied as per normal expected timings. The bug was identified and posted to the devs within 3 days, which we consider fast enough on public forums. Those speeds might not meet your expectations, but that’s as fast as we can be right now. The actual bug, however took a year to be fixed, only because it was a very minor impact. The problem in the thread you linked was only when uploading functions without uploading any files. This has nothing to do with your case.
Again, you’re holding it wrong, I’m afraid.
To clarify, forums don’t have any response time guarantee. We try to be as fast as possible, without any assurance. In the helpdesk, we try to get back to Pro tier within 3 to 4 days (though most responses are faster than that). Furthermore, the post is only 17 hours old right now. You might have spent 24 hours before posting, but unfortunately that cannot be used to count our response time.
Incorrect endpoint.
You’re right.
It talks about deploying via ZIP. In the docs you’ve linked we have not mentioned you can create and deploy in the same API call for deploying via file-digest. Sorry to say, but there’s a chace you might have assumed some things and thus, it did not work as expected.
This has been mentioned in the docs under File digest method. Quoting:
We recommend using a digest including a file path and SHA1 for each item. This method also allows you to upload serverless functions, however serverless functions should use SHA256 instead.
Answer to the last response here
This could be very much due to the invalid zip that you’re generating. If you share the response headers that could include a request ID of some kind, we can check our logs for that.
Nowhere is it mentioned that you have to use the endpoint https://api.netlify.com/api/v1/sites/site_id/deploys to upload functions.
When creating a new site, you can include a file digest or a ZIP file straight away, to save an HTTP request.
Please amend the documentation so that it says that you cannot upload functions through this endpoint. To create a great developer experience, I shouldn’t have to “figure it out” as you stated.
I actually did test the bundling of my function using the command netlify build and it bundled the function into a zip with no other file contents apart from [FUNCTION_NAME].js so I’ve already investigated this. There is no difference between me using the zip-it-and-ship-it package. There is a way I could add ESM support to use the package directly but it affects other parts of my setup and I have no desire to. As I explained my function has zero third-part dependencies. I used Postman to upload the zip file created bynetlify build and the operation is still failing with the same error.
The OpenAPI schema says to send the response body (which is meant to be a zip) as application/octet-stream which I have seen others become confused about and I was as well. Reading the OpenAPI schema doesn’t say anything about this. The documentation simply says that the file should be zipped before uploading without specifying how to bundle it either using netlify build.
The OpenAPI schema does support examples, since the Netlify API is not a massive API I would encourage your team to invest the time to add examples (you can add a single inline example or multiple examples to each endpoint). While this may take you a few days to do, it will likely result in far less tickets being opened here and actually save you time and effort.
I am willing to create an end-to-end documentation guide for how to use the API manually if I am assigned credit and you can help me past this last impasse.
For the time being I have swapped to using automatic branch deploys since this seems to be better supported but my wish was to do everything programatically as part of a reproducible pipeline without doing anything in the Netlify UI.
The 500/400 error message I posted regarding my attempts to upload the zip files is uninformative and unhelpful. Please have the 500 error that it turn references the Functions 400 Bad Request error actually try to report some information.
I will try again with a request ID and reference it in the next message.
You’re right. I edited my response above to say what I meant. I’m used to using ESM in my projects, so I just tried with the values I was using instead of the defaults. If you’re using a JS file in CJS format, simply adding it to zip should work.
That’s about zip file method, which you’re not supposed to use for functions.
I am not referring to the fact that the File digest method documentation text says that you can upload serverless functions. I am referring to the fact that:
When creating a new site, you can include a file digest or a ZIP file straight away, to save an HTTP request.
Does not make it clear enough that you cannot upload functions through the sites endpoint to save an HTTP request. Do you understand the nuance?
That’s about zip file method, which you’re not supposed to use for functions.
Actually I was referring to Netlify API documentation which is absolutely used for function upload. I am not referring to the zip file method of uploading files.
To explain my use case:
A user clicks “Deploy Site” in my own web application.
A site is created
The function and file digest is used to create a deploy.
The deploy response returns required_functions and required.
Any missing files are uploaded to Netlify using the appropriate endpoints.
Environment variables specific to each user are set at a site level.
A subdomain is created for the customer at customerId.subdomain.domain.com
Since the automatic branch subdomain deployments seems to be an easier solution I have moved towards that. The only issue I’m having there is that I’m unsure of at what point in the deployment process the environment variables need to be set. My current intention is to use a webhook for Deploy succeeded to update the environment variables at a branch level (which is supported in documentation) once Netlify responds to the new branch on Github. The steps look like this, though I realize this might be a tangent from the original issue which I still desire to resolve. I assume that having a separate Git branch for each customer might just be a better solution in general since we can easily update it for them and everything will be tracked in version control.
User clicks “Deploy Site” in my own web application
The backend uses a Github access token to create a new branch reference
Once the Netlify automatic subdomain branch deployment is complete, send a webhook to my backend handler endpoint.
I’d personally recommend creating a separate site from each branch as in the long run, that would probably be easier to manage. But if you wish to do it all on the current site, you can also create variables for the branch before creating a branch on GitHub, so you can skip that webhook part. The payload would look:
Thanks, that is great to know, I really appreciate it. I’d still like to resolve the original issue but since I am time and effort constrained at the moment I think I will folllow the automatic deploy approach which offers more benefits and seems more robust given the number of customers using it vs the manual uploads.
I will re-attempt the manual upload when I have time and will update this thread after re-reading your responses and trying again.
@hrishikesh Everything is working well, however I’m using a Netlify serverless function with function-scoped branch context based API keys for OpenAI. This is then safely passed to my server (server - server) without exposing it to the client-side code.
The API requests can take some time to execute (particularily on a cold start to my own server). I see the current limit is 10 seconds. I’m definitely going to need this increased to the Pro tier maximum of 26 seconds as I’ve seen mentioned elsewhere.
I believe that this should suffice (a chat response longer than this is pretty useless anyway) but in the event that I need even more can you let me know the pricing privately? From my experience AWS Lambda has a timeout of 15 minutes.
The other option is to make the request asynchronous and post the result to a websocket when it is done.
Let me know if you’d prefer me to open another issue.
On average it is taking 15 seconds for the cold function to warm-up. Could you increase to the limit for the Pro plan? I just sent an email to support. As we have more usage on our platform, this delay will fall because there will always be a function instance running, but we need this for launch of our product in the first few weeks to months.