Overview

Step by step model upload example

1. Visit https://www.cgtrader.com/oauth/applications/new and create new oauth application.

2. Make sure you have environment variables set to something like this:

    export CLIENT_ID=".." # Application Id
    export CLIENT_SECRET=".." # Secret
    export API_BASE="https://api.cgtrader.com/"
    export REDIRECT_URI=".."
    export IMAGE_PATH="/absolute/path/to/image.png"
    export FILE_PATH="/absolute/path/to/model.max"
  

3. Initialize oauth client. In Ruby programming language it looks like this:

    require 'oauth2'

    client = OAuth2::Client.new(ENV['CLIENT_ID'], ENV['CLIENT_SECRET'], site: ENV['API_BASE']) do |stack|
      stack.request :multipart
      stack.request :url_encoded
      stack.adapter Faraday.default_adapter
    end
  

4. Generate authorization url with the following code:

    authorize_url = client.auth_code.authorize_url(redirect_uri: ENV['REDIRECT_URI'])
    puts "Log in to application and open: \n#{authorize_url}"
  

5. Log in to cgtrader.com and visit authorization url in the browser. Acquire access token with authorization_code displayed in the browser:

    puts "\nEnter authorization code:"
    authorization_code = gets
    access_token = client.auth_code.get_token(authorization_code.strip, redirect_uri: ENV['REDIRECT_URI'])
  

6. Post new model attributes to the API endpoint and store model id from the result:

    model =  {
      "category" => "aircraft",
      "subcategory" => "aircraft-part",
      "description" => "description",
      "license" => "royalty_free",
      "title" => "title",
      "tags" => [ "tag1", "tag2", "tag3", "tag4", "tag5" ]
    }

    puts "Creating new model"
    response = access_token.post('v1/models', body: model)
    model_id = JSON.parse(response.body)['id']
    puts "Success, id = #{model_id}"
  

7. Attach model images & files to the newly created model:

    upload = Faraday::UploadIO.new(ENV['IMAGE_PATH'], nil)
    response = access_token.post("v1/models/#{model_id}/images", params: {}, body: { image: upload })
    puts "Image upload success: #{JSON.parse(response.body)}"

    upload = Faraday::UploadIO.new(ENV['FILE_PATH'], nil)
    response = access_token.post("v1/models/#{model_id}/files", params: {}, body: { file: upload })
    puts "File upload success: #{JSON.parse(response.body)}"
  

8. When you are finished with editing, update model status

    puts "Update model status to published"
    response = access_token.put("v1/models/#{model_id}", body: { status: 'published' })
    puts "Success, open #{JSON.parse(response.body)['url']}"
  

9. Now you should be able to find model in the https://www.cgtrader.com/3d-models