Importing assets and media files to Optimizely through API


We have around 50000 images that need to be imported into Optimizely CMS 12. Is there any way we can do this through an API like Content Management API? if not what are the other possible options?

Nov 19, 2021 12:22

Hi Dejan Caric, Service API documentation mentions it is for importing assets into Optimizely commerce. But we don't have commerce in our project 

Nov 19, 2021 13:31


You don't need Commerce to use Service API. It works with CMS too.

Nov 19, 2021 15:10

I've tested this with Alloy (CMS 11). Should be the same for CMS 12.

Test app:

internal class Program
    private static void Main(string[] args)
        string token = GetToken();

        using (var client = new HttpClient())
            client.BaseAddress = new Uri(@"https://localhost:44381/");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var content = new MultipartFormDataContent();
            var filestream = new FileStream(@"c:\temp\", FileMode.Open);
            content.Add(new StreamContent(filestream), "file", "");
            var response = client.PostAsync("/episerverapi/commerce/import/assets", content).Result;
            Console.WriteLine(response.StatusCode.ToString()); }

    private static string GetToken()
        using (var client = new HttpClient())
            client.BaseAddress = new Uri(@"https://localhost:44381/");
            var fields = new Dictionary<string, string>
                { "grant_type", "password" },
                { "username", "admin" },
                { "password", "Administrator123!" }
            var response = client.PostAsync("/episerverapi/token", new FormUrlEncodedContent(fields)).Result;
            if (response.StatusCode == HttpStatusCode.OK)
                var content = response.Content.ReadAsStringAsync().Result;
                var token = JObject.Parse(content).GetValue("access_token");

                return token.ToString();

            return string.Empty;

C:\temp\ contains MediaImport.xml that looks like this:

  <MediaType name="ImageFile">
    <ContentType>AlloyFind.Models.Media.ImageFile, AlloyFind</ContentType>
  <MediaGroup mediaTypeName="ImageFile">
      <Name>Test Image.png</Name>

Here I want to import an external image to For All Sites folder.

And here's how it looks in action:

Hope this helps!

Nov 19, 2021 15:29

Hi Dejan, Thanks for helping. Can you clarify one more doubt? Does Service API migrate assets to BLOB providers like Azure when they were added to the application? if so does that require any additional configuration other than adding a default provider on app settings?

Nov 22, 2021 15:36

The Service API uses IContentRepository under the hood.

As long as your Optimizely instance is properly configured for Azure/DXP, you don't need to configure any additional/service api-specific app settings.

Nov 22, 2021 16:02
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.