Home
Support Forums

Generate previews on demand via Netlify's API with Github integration

We are building a CMS for sites deployed on Netlify. A feature we want to add is the ability to generate a preview for the site configuration. Each site has a Github repo connected to the Netlify site and during build time it recovers configuration from an external DB.

In my deploys, code in the Github repo might not change, but the external configuration can. So we want to be able to trigger (via the Netlify API) a preview of the current changes without having to create branches and PRs.

I tried using the createSiteDeploy method from the API but previews return a 404. Creating builds with createSiteBuild will build and redeploy the entire site on the master branch, which is not what we want.

We want to deploy without affecting the current deployment.

Thanks!

Hey there, @GonzaloHirschToptal :wave:

Thanks for reaching out! Good question.

At this time, it is not possible to generate just a deploy preview via the API. This is because we consider deploy previews to be only deploys created via git for a merge request or a pull request. If you are instead talking about a “browsable deploy that is not published at the production URL”, then yes, you can absolutely create a deploy like that via the API! But, it will not be a deploy preview and will not have Collaborative Deploy Preview features, for example.

I spoke to a team member, and they recommend using the CLI and deploying a “draft” deploy, rather than using the API directly. I’ve linked to our CLI docs here so that you can read further! Let me know if this helps :slight_smile:

@hillary we are restricted to using only the API, we don’t care about the collaborative features in the preview, we just want to be able to build previews/deploys on demand via the API. That being said, the previews we need shouldn’t be published to the production URL.

I tried creating draft previews but I was unsuccessful. What specific endpoint should I use for this. I tried the createSiteDeploy and createSiteBuild.

Hi @GonzaloHirschToptal,

Have you checked this:

@hrishikesh yes, I checked that part of the docs and tried implementing it, but the problem is that we don’t have the files to be sent for deployment. We want to trigger a deployment with files from Github, is that possible without having to clone the repo or something like that?

I’d like to be able to generate a site deploy (as a draft, so it doesn’t affect the production url) from the contents of an already linked Github repo (without having to clone it).

So, if you are always going to use the latest commit of the repo, you could simply use the following call:

https://api.netlify.com/api/v1/sites/<site-id>/builds. Send a POST request to that and Netlify will build for the latest commit of your repo. However, if you want to deploy for a specific commit, that’s not currently possible.

@hrishikesh from my understanding that endpoint affects the production URL, right?

We want to use the latest commit, but I’ve tried that endpoint and it affected the production URL and that’s not what we wanted.

Yes, it would trigger a change on the production as @hillary said, there’s no real way to trigger a PR from API.

So your workflow would be:

Build a production deploy → Lock publishing (possible via API) → Tigger the preview builds as explained above → Since publishing is locked, the content on production URL won’t change, so if you wish to change it trigger yet another deploy same as above → Publish it (again, via API).

Basically, you can build multiple times, but publish it live only once as you need it.

1 Like

@hrishikesh interesting, I wasn’t aware of locking the deploys, thank you, this’ll probably work.

1 Like